C++ 编译器错误:无法从对象类型转换<;T>;对象类型<;T>;在c++;
在我正在编写的共享指针类中有一个方法C++ 编译器错误:无法从对象类型转换<;T>;对象类型<;T>;在c++;,c++,templates,compiler-errors,C++,Templates,Compiler Errors,在我正在编写的共享指针类中有一个方法 template<class T> template<class Y> shared_ptr<T> &shared_ptr<T>::operator=(/*const*/ shared_ptr<Y> &r) { shared_ptr<T>(r).swap(*this); return *this; } 模板 模板 shared_ptr&shared_ptr::o
template<class T>
template<class Y>
shared_ptr<T> &shared_ptr<T>::operator=(/*const*/ shared_ptr<Y> &r)
{
shared_ptr<T>(r).swap(*this);
return *this;
}
模板
模板
shared_ptr&shared_ptr::operator=(/*const*/shared_ptr&r)
{
共享ptr(r).交换(*本);
归还*这个;
}
以这种方式使用时
class Foo
{
};
class Bar : public Foo
{
};
int main(int /*argc*/, char * /*argv*/[]) {
shared_ptr<Foo> giPtr(new Foo(1));
shared_ptr<Bar> giPtr2;
giPtr2 = glext::dynamic_pointer_cast<Foo>(giPtr);
}
class-Foo
{
};
分类栏:公共食品
{
};
int main(int/*argc*/,char*/*argv*/[]){
共有(新富(1));;
共享ptr giPtr2;
giPtr2=glext::动态指针投射(giPtr);
}
在MSVC中生成以下错误:
1>c:\users\mehoggan\documents\github\x86-applications\glextensions\smart_ptrs\shared_ptr\shared_ptr.inl(53): error C2440: '<function-style-cast>' : cannot convert from 'glext::shared_ptr<T>' to 'glext::shared_ptr<T>'
1> with
1> [
1> T=Foo
1> ]
1> and
1> [
1> T=Bar
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1> main.cpp(28) : see reference to function template instantiation 'glext::shared_ptr<T> &glext::shared_ptr<T>::operator =<Foo>(glext::shared_ptr<Foo> &)' being compiled
1> with
1> [
1> T=Bar
1> ]
1>c:\users\mehoggan\documents\github\x86 applications\glextensions\smart\u ptrs\shared\u ptr\shared\u ptr.inl(53):错误C2440:“”:无法从“glext::shared\u ptr”转换为“glext::shared\u ptr”
1> 与
1> [
1> T=Foo
1> ]
1> 及
1> [
1> T=巴
1> ]
1> 没有构造函数可以采用源类型,或者构造函数重载解析不明确
1> main.cpp(28):请参阅对正在编译的函数模板实例化“glext::shared_ptr&glext::shared_ptr::operator=(glext::shared_ptr&)”的引用
1> 与
1> [
1> T=巴
1> ]
构造函数
模板
shared_ptr::shared_ptr():
_px(0),
_pn()
{}
模板
模板
shared_ptr::shared_ptr(Y*p):
_px(p),
_pn()
{
内部::共享指针构造(this,p,_pn);
}
//TODO:从弱指针创建共享ptr
模板
共享\u ptr::共享\u ptr(常量共享\u ptr&r):
_px(r.u px),
_注册护士(注册护士)
{}
模板
模板
共享ptr::共享ptr(常量共享ptr&r,元素类型*p):
_px(p),
_注册护士(注册护士)
{}
模板
共享\u ptr和共享\u ptr::运算符=(常量共享\u ptr&r)
{
共享ptr(r).交换(*本);
归还*这个;
}
交换
模板
void shared_ptr::swap(shared_ptr&other)
{
标准::交换(_-px,其他._-px);
_请注意:互换(其他);
}
MSVC经常会遇到这个问题
您可以尝试使用“injected”名称,而不是返回类型的模板id:
template<class T>
template<class Y>
shared_ptr<T> &shared_ptr<T>::operator=(/*const*/ shared_ptr<Y> &r)
{
shared_ptr(r).swap(*this); // note: no <T> there
return *this;
}
模板
模板
shared_ptr&shared_ptr::operator=(/*const*/shared_ptr&r)
{
shared_ptr(r).swap(*this);//注意:没有
归还*这个;
}
我不是很确定,但你可能会幸运地在课堂上定义这一点:
模板类共享\u ptr
{
模板
共享\u ptr&运算符=(共享\u ptr&r)
{
shared_ptr(r).swap(*this);//注意:没有
归还*这个;
}
// ...
这里我有点猜测,但我认为在定义方法体时类可能“看起来”不完整。我不认为这应该是个问题,但MSVC两阶段查找1是出了名的…麻烦
1(基本上是模板实例化机制)我们需要查看您的构造函数您想用
交换方法做什么?定义说明了一切。请参阅上面的定义。请注意=
运算符的第二个定义与第一个定义相同,尽管它专门用于Y
=T
的情况。对不起,我没有看到任何定义swap
。这样就消除了一个错误,现在当我将常量放回以使其语义正确时,我得到以下错误。错误C2440:“”:无法从“const glext::shared_ptr”转换为“glext::shared_ptr”
template<class T>
void shared_ptr<T>::swap(shared_ptr<T> &other)
{
std::swap(_px, other._px);
_pn.swap(other._pn);
}
template<class T>
template<class Y>
shared_ptr<T> &shared_ptr<T>::operator=(/*const*/ shared_ptr<Y> &r)
{
shared_ptr(r).swap(*this); // note: no <T> there
return *this;
}
template<class T> class shared_ptr
{
template<class Y>
shared_ptr& operator=(shared_ptr<Y> &r)
{
shared_ptr(r).swap(*this); // note: no <T> there
return *this;
}
// ...