2013-08-25 118 views
3

我有以下课程,我想添加到map作为shared_ptr无法使用std :: shared_ptr作为std :: map中的值类型?

struct texture_t 
{ 
hash32_t hash; 
uint32_t width; 
uint32_t height; 
uint32_t handle; 
}; 

所以我尝试使用make_pair然后将其添加到map ...

auto texture = std::make_shared<texture_t>(new texture_t()); 
std::make_pair<hash32_t, std::shared_ptr<texture_t>>(hash32_t(image->name), texture); 

而且在make_pair,我收到以下编译错误:

error C2664: 'std::make_pair' : cannot convert parameter 2 from 'std::shared_ptr<_Ty>' to 'std::shared_ptr<_Ty> &&' 

我觉得像我失去了明显的东西,任何线索?

回答

5

std::make_pair不适用于显式模板参数。请将它们关闭:

auto my_pair = std::make_pair(hash32_t(image->name), texture); 

注意:对make_shared的调用也是错误的。参数传递给构造函数texture_t,所以在这种情况下它只会是:

auto texture = std::make_shared<texture_t>(); 
+0

修好了吧,谢谢! :) –

+0

对'make_shared'的调用也是错误的。这已经在[OP的后续问题](http://stackoverflow.com/questions/18433712/trouble-constructing-shared-ptr/)中得到解决,但在此可能值得一提的是完整性。 – juanchopanza

+0

@juanchopanza:谢谢! –

相关问题