我的任务有问题。我试图从文件中读取每个写入的行对应于一个对象(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
可能仍然是其中列出的原因之一(它非常完整)。你能显示你的链接器命令行吗? (可能是例如目标文件顺序的问题)。 –