我的任務有問題。我試圖從文件中讀取每個寫入的行對應於一個對象(ShapePtr),並且應該從每行初始化一個對象並將它們存儲在一個列表中。所有的ShapePtr都有一個指向形狀的指針。從文件中讀取對象時未定義的符號
這裏距離主要的方法是抱怨存根:
ifstream is("test.dat");
istream_iterator<ShapePtr> shapein(is), endofshapein;
list<ShapePtr> shapelist(shapein, endofshapein);
for (list<ShapePtr>::iterator it = shapelist.begin(); it != shapelist.end(); ++it)
cout << *it << endl;
ShapePtr的定義:
class ShapePtr
{
private:
Shape *shape;
public:
ShapePtr() { shape = 0; }
ShapePtr(const ShapePtr& shptr);
ShapePtr& operator=(const ShapePtr& shptr);
ShapePtr(Shape *ptr) { shape = ptr; }
~ShapePtr() { delete shape; }
ShapePtr* clone() const;
friend ostream& operator<<(ostream& out, const ShapePtr& sh);
friend istream& operator>>(istream& in, ShapePtr& sh);
};
inline ostream& operator<<(ostream& out, const ShapePtr& sh)
{
sh.shape->print(out);
return out;
}
inline istream& operator>>(istream& in, ShapePtr& sh)
{
string name, data;
in >> name;
getline(in, data);
cout << name << '\n';
cout << data << endl;
//sh.shape->read(in); // Not currently implemented
sh = ShapePtr(new Point(1.0, 2.0, 3.0)); // Just for testing
return in;
}
ShapePtr的實施:
ShapePtr::ShapePtr(const ShapePtr& shptr)
{
if(shptr.shape)
shape = shptr.shape->clone();
else
shape = 0;
}
ShapePtr& ShapePtr::operator=(const ShapePtr& shptr)
{
if(shptr.shape)
shape = shptr.shape->clone();
else
shape = 0;
return *this;
}
ShapePtr *ShapePtr::clone() const
{
return new ShapePtr(*this);
}
即時通訊目前只是試圖查看從文件中讀取什麼,以便我可以繼續執行初始化n的形狀。但是現在當我嘗試運行時,出現以下錯誤:
Undefined symbols for architecture x86_64:
"ShapePtr::ShapePtr(ShapePtr const&)", referenced from:
std::__1::list<ShapePtr, std::__1::allocator<ShapePtr> >::push_back(ShapePtr const&) in main-1a9984.o
std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long>::istream_iterator(std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long> const&) in main-1a9984.o "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, ShapePtr const&)", referenced from:
_main in main-1a9984.o "operator>>(std::__1::basic_istream<char, std::__1::char_traits<char> >&, ShapePtr&)", referenced from:
_main in main-1a9984.o std::__1::list<ShapePtr, std::__1::allocator<ShapePtr> >::list<std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long> >(std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long>, std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long>, std::__1::enable_if<__is_input_iterator<std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long> >::value, void>::type*) in main-1a9984.o
ld: symbol(s) not found for architecture x86_64
我在做什麼錯?我試圖自己解決這個問題,但我無處可去。由於
編輯:
我的問題是什麼:不包括編譯命令文件shapeptr.cpp。
錯誤消息非常清晰,您必須定義複製構造函數:'ShapePtr :: ShapePtr(ShapePtr const&)'。你似乎錯過了它。 –
@πάνταῥεῖ我編輯了這個問題。它仍然是重複的嗎? – patriques
可能仍然是其中列出的原因之一(它非常完整)。你能顯示你的鏈接器命令行嗎? (可能是例如目標文件順序的問題)。 –