【问题标题】:std::make_shared gives error i dont understandstd::make_shared 给出了我不明白的错误
【发布时间】:2014-02-16 12:55:56
【问题描述】:

我的地图如下所示:

typedef std::map<PuzzlePartLocation, std::shared_ptr<PuzzleObj>> ComponentsMap;

现在我尝试通过这样的功能使用元素设置此地图:

void ComponentMadiator::Register(const PuzzlePartLocation puzzlePartLocation,PuzzleObj* puzzleObj)
{
   componentsMap[puzzlePartLocation] = std::make_shared<PuzzleObj>(puzzleObj);
}

我只是这样称呼它:

 PuzzleObj* pPuzzleObjStickLeft = new PuzzleObj()
pComponentMadiator->Register(1,pPuzzleObjStickLeft );

PuzzleObj 包含来自 IImageComponent 类型的成员 *
PuzzleObj 继承自基类

但它给了我这样的错误:

1>c:\program files\microsoft visual studio 11.0\vc\include\memory(873): error C2664: 'PuzzleObj::PuzzleObj(IImageComponent *)' : cannot convert parameter 1 from 'PuzzleObj *' to 'IImageComponent *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>          c:\program files\microsoft visual studio 11.0\vc\include\memory(972) : see reference to function template instantiation 'std::_Ref_count_obj<_Ty>::_Ref_count_obj<PuzzleObj*&>(_V0_t)' being compiled
1>          with
1>          [
1>              _Ty=PuzzleObj,
1>              _V0_t=PuzzleObj *&
1>          ]
1>          c:\program files\microsoft visual studio 11.0\vc\include\memory(972) : see reference to function template instantiation 'std::_Ref_count_obj<_Ty>::_Ref_count_obj<PuzzleObj*&>(_V0_t)' being compiled
1>          with
1>          [
1>              _Ty=PuzzleObj,
1>              _V0_t=PuzzleObj *&
1>          ]
1>          d:\dev\cpp\cocos2d-x\cocos2d-x-3.0beta2\cocos2d-x-3.0beta2\projects\neonbreaker\classes\componentmadiator.cpp(23) : see reference to function template instantiation 'std::shared_ptr<_Ty> std::make_shared<PuzzleObj,PuzzleObj*&>(_V0_t)' being compiled
1>          with
1>          [
1>              _Ty=PuzzleObj,
1>              _V0_t=PuzzleObj *&
1>          ]

【问题讨论】:

  • 如果你想复制puzzleObjComponentMadiator::Register,你需要std::make_shared&lt;PuzzleObj&gt;(*puzzleObj);
  • 为什么能解释一下?
  • PuzzleObj 有一个接受IImageComponent* 的构造函数,对吧?
  • 是的,PuzzleObj 也扩展了名为 BasePuzzleObj 的基类

标签: c++ shared-ptr


【解决方案1】:

std::make_shared&lt;PuzzleObj&gt; 为您创建一个新的PuzzleObj。你需要的是std::shared_ptr&lt;PuzzleObj&gt;(puzzleObj)

更重要的是

void ComponentMadiator::Register(const PuzzlePartLocation puzzlePartLocation,PuzzleObj* puzzleObj);

应该声明为:

void ComponentMadiator::Register(const PuzzlePartLocation puzzlePartLocation, std::shared_ptr<PuzzleObj> const& puzzleObj);

因为它通过将puzzleObj 存储在容器中来共享其所有权,并且必须在函数的接口中进行通信。

然后这样称呼它:

std::shared_ptr<PuzzleObj> pPuzzleObjStickLeft(std::make_shared<PuzzleObj>());
pComponentMadiator->Register(1, pPuzzleObjStickLeft);

【讨论】:

  • 它给了我这个错误:1>d:\dev\cpp\cocos2d-x\cocos2d-x-3.0beta2\cocos2d-x-3.0beta2\projects\neonbreaker\classes\helloworldscene.cpp (92): 错误 C2664: 'ComponentMadiator::Register' : 无法将参数 2 从 'PuzzleObj *' 转换为 'const std::shared_ptr<_ty> &'
猜你喜欢
  • 2021-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多