2013-05-08 79 views
-2

你好我是新來的C++,使用std :: string中的boost共享指針來分配變量

我想在字符串上使用共享指針。

class Test{ 
private: 
void test(std::string* str); 
boost::shared_ptr<std::string> sample; 
}; 

void Test::test(std::string* str) { 

sample.reset(str); //This is not working. 
} 

任何人都可以告訴我什麼是錯?

+1

不你的編譯器告訴你什麼是錯的? – Nick 2013-05-08 07:51:20

+3

不是你的問題,但在字符串上使用shared_ptr看起來像是矯枉過正。你有任何理由不只是使用純字符串,'std :: string sample;'?除了其他任何東西'std :: string'都可以使用引用計數來實現,在這種情況下'boost :: shared_ptr '只是一種浪費。 – john 2013-05-08 07:53:26

+2

它不起作用?調用者創建的'str'參數如何? – hmjd 2013-05-08 07:55:23

回答

2

首先聲明

void test(); 

然後你實現

void Test::test(std::string* str) { 

這不應該工作

+0

對不起,錯別字 – Kathick 2013-05-08 07:53:09

+1

好吧,那麼請將編譯器錯誤消息添加到您的問題 – spiritwolfform 2013-05-08 07:55:34