2013-08-25 120 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:謝謝! –

相關問題