有人請解釋爲什麼我在這裏得到編譯錯誤 - 錯誤C2558:class'std :: auto_ptr < _Ty>':沒有拷貝構造函數可用或拷貝構造函數聲明爲 '顯性'沒有拷貝構造函數可用或拷貝構造函數被聲明爲'explicit'
#include <memory>
#include <vector>
#include <string>
template<typename T>
struct test
{
typedef std::auto_ptr<T> dataptr;
typedef std::auto_ptr< test<T> > testptr;
test(const T& data):
data_(new T(data))
{
};
void add_other(const T& other)
{
others_.push_back(testptr(new test(other)));
}
private:
dataptr data_;
std::vector<testptr> others_;
};
int main(int argc, char* argv[])
{
test<std::string> g("d");
//this is the line that causes the error.
g.add_other("d");
return 0;
}
我已經完成了C++,但不應該是'g = test(「d」);'? –
2010-08-06 00:09:56
@Jesse J:兩個都沒問題。這兩種方式的行爲略有不同,除了最惡劣的情況外,其他行爲都有相同的結果。從技術上講,你的方法將創建一個測試類,然後將其分配給g,而不僅僅是初始化g本身。當你有自定義的複製/分配/初始化行爲時,這隻會成爲一個問題。 – Akusete 2010-08-06 00:32:34
謝謝大家。真正的信息答案。 – Carl 2010-08-06 01:07:19