【问题标题】:compiler error: cannot convert from object_type<T> to object_type<T> in c++编译器错误:无法在 c++ 中从 object_type<T> 转换为 object_type<T>
【发布时间】:2013-08-05 00:04:20
【问题描述】:

我正在编写的共享指针类中有一个方法。

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;
}

当以这种方式使用时

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);
} 

在 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>          ]

构造函数

template<class T>
  shared_ptr<T>::shared_ptr() : 
    _px(0), 
    _pn()
  {}

  template<class T>
  template<class Y>
  shared_ptr<T>::shared_ptr(Y * p) : 
    _px(p), 
    _pn()
  {
    internal::shared_pointer_construct(this, p, _pn);
  }

  // TODO: Create shared ptr from weak pointer

  template<class T>
  shared_ptr<T>::shared_ptr(const shared_ptr &r) :
    _px(r._px),
    _pn(r._pn)
  {}

  template<class T>
  template<class Y>
  shared_ptr<T>::shared_ptr(const shared_ptr<Y> &r, element_type *p) :
    _px(p), 
    _pn(r._pn)
  {}

  template<class T>
  shared_ptr<T> &shared_ptr<T>::operator=(const shared_ptr<T> &r)
  {
    shared_ptr<T>(r).swap(*this);
    return *this;
  }

交换

template<class T>
void shared_ptr<T>::swap(shared_ptr<T> &other)
{
  std::swap(_px, other._px);
  _pn.swap(other._pn);
}

【问题讨论】:

  • 我们需要看看你的构造函数
  • 你想用swap 方法做什么?
  • 定义说明了一切。参见上面的定义。
  • 请注意,= 运算符的第二个定义与第一个定义相同,尽管它专门针对 Y = T 的情况。
  • 抱歉,我没有看到 swap 的任何定义。

标签: c++ templates compiler-errors


【解决方案1】:

这个问题经常出现在 MSVC 中。

您可以尝试使用“注入”名称,而不是返回类型的模板 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;
}

我不太确定,但您可能会幸运地在课堂上定义这个

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;
    }
    // ...

我在这里猜测了一下,但我认为该类在定义方法体时可能“显得”不完整。我不认为这应该是个问题,但是 MSVC 两阶段查找1 是出了名的......麻烦。


1(基本上是模板实例化机制)

【讨论】:

  • 消除了一个错误,现在当我将 const 重新放入以使其在语义上正确时,我得到以下错误。错误 C2440:“”:无法从“const glext::shared_ptr”转换为“glext::shared_ptr
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多