我自己的智能指针模板编译错误
我正在执行斯科特·迈耶斯书中的以下简单程序。我正在使用 Visual studio 2009 进行编译。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Top { };
class Middle: public Top { };
class Bottom: public Middle { };
template<typename T>
class SmartPtr {
public:
template<typename U>
SmartPtr(const SmartPtr<U>& other) : heldPtr(other.get()) { }
T* get() const { return heldPtr; }
private:
// built-in pointer held
T *heldPtr;
};
int main()
{
SmartPtr<Top> pt1 = SmartPtr<Middle>(new Middle); // SmartPtr<Top>
}
在编译过程中,我收到以下错误,
1>d:\technical\c++study\addressconv.cpp(36) : error C2440: '<function-style-cast>' : cannot convert from 'Middle *' to 'SmartPtr<T>'
1> with
1> [
1> T=Middle
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>d:\technical\c++study\readparsing\readparsing\addressconv.cpp(36) : error C2512: 'SmartPtr<T>' : no appropriate default constructor available
1> with
1> [
1> T=Top
1> ]
请请求帮助解决问题。问题的根本原因是什么?
谢谢!
I am having following simple program from scott meyers book. I am compiling using Visual studio 2009.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Top { };
class Middle: public Top { };
class Bottom: public Middle { };
template<typename T>
class SmartPtr {
public:
template<typename U>
SmartPtr(const SmartPtr<U>& other) : heldPtr(other.get()) { }
T* get() const { return heldPtr; }
private:
// built-in pointer held
T *heldPtr;
};
int main()
{
SmartPtr<Top> pt1 = SmartPtr<Middle>(new Middle); // SmartPtr<Top>
}
During compilation i am getting following error
1>d:\technical\c++study\addressconv.cpp(36) : error C2440: '<function-style-cast>' : cannot convert from 'Middle *' to 'SmartPtr<T>'
1> with
1> [
1> T=Middle
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>d:\technical\c++study\readparsing\readparsing\addressconv.cpp(36) : error C2512: 'SmartPtr<T>' : no appropriate default constructor available
1> with
1> [
1> T=Top
1> ]
Kindly request to help in resolving problem. What is root cause of problem?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要实现一个接受
U*
的构造函数。它抱怨它无法显式地将U*
转换为SmartPtr
。You need to implement a constructor that accepts a
U*
. It's complaining that it cannot explicitly convert aU*
to aSmartPtr<U>
.首先,创建两个复制构造函数。一个接受相同类型,另一个接受可以动态转换为基本类型的任何其他类型。这就是我的意思。
请记住,根据您的代码,基类指针没有与派生类 SmartPtr 保持相同的引用计数。这意味着如果基类或派生类 SmartPtr 超出范围,指针将变得无效。
编辑:这绝对有效。基本问题是没有一个构造函数以指针作为参数来创建 SmartPtr。这是工作代码。
我希望这有帮助。我还将基类的析构函数设为动态的,因为这是任何要继承的类所必需的。
First of all, create two copy constructors. One to accept the same type and the other to accept any other type that can be dynamically casted to the base type. Here's what I mean.
Do keep in mind that according to your code, the base class pointer is not keeping the same reference count as the derived class SmartPtr. Which mean if either the base-class or derived-class SmartPtr goes out of scope, the pointer will become invalid.
EDIT: This definitely works. The basic issue was there wasn't a constructor that took a pointer as argument to create a SmartPtr. Here's the working code.
I hope this helps. I also made the destructors of the base classes dynamic because that's required of any classes that are intended to be inherited.
您遇到的错误很简单:没有
SmartPtr
的构造函数将简单的T*
(或U*
)作为参数。添加以下内容:您的代码应该可以编译。
关于复制构造函数:确保您传输所有权或实现引用计数,否则您会遇到麻烦。
The error you have is simple: there is no constructor of
SmartPtr
taking a simpleT*
(orU*
) as a parameter. Add the following:and your code should compile.
With regard to the copying constructors: make sure you either transmit ownership or implement reference counting, otherwise you'll be in trouble.