从文件中读取对象时未定义的符号
Undefined symbols while reading in objects from file
我对作业有问题。我试图从一个文件中读取,每个写入行对应于一个对象(shapetr),我应该从每行初始化一个对象,并将它们存储在一个列表中。所有的shapetr都有一个指向形状的指针。
下面是主方法的存根:
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;
shapetr的定义:
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;
}
shapetr:
的实现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);
}
我目前只是想看看从文件中读取什么,所以我可以继续实现形状的初始化。但是现在,当我尝试运行时,我得到以下错误:
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
我做错了什么?我试图解决这个问题,但我无处可去。由于
编辑:
我的问题是:没有在编译命令中包含shapetrp .cpp文件
您声明了ShapePtr
的复制构造函数,但没有实现它。要么实现它,要么删除原型以使用默认原型。
既然您定义了ShapePtr::~ShapePtr()
,那么您应该将其与ShapePtr::operator=()
一起实现以获得预期的行为
相关文章:
- vscode g++链路故障:体系结构x86_64的未定义符号
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- C++ 中 dlsym 的未定义符号
- 仅在 MacOS 上析构函数的未定义符号
- OSX clang++:用于 cpp 文件中显式实例化模板的体系结构x86_64的未定义符号
- PCL 出错:体系结构x86_64 @pcl的未定义符号
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 体系结构x86_64的未定义符号:链接器错误
- C++ std::vector<int> 体系结构的未定义符号 x86_64:
- 生成文件:体系结构x86_64的未定义符号
- libc++abi未定义符号:_ZTVN10__cxxabif120__si-class_type_infoE
- Xcode"体系结构x86_64的未定义符号"
- 体系结构x86_64的未定义符号:找不到原因
- SWIG链接器:未定义符号:_ZN2cv8fastFreeEPv(cv::fastFree(void*))
- 如何修复架构x86_64的未定义符号,Boost Asio 1.58
- 在Qt5上去静态库:架构的未定义符号x86_64:
- 体系结构x86_64的未定义符号:c++/ h 文件和 cpp 文件
- 监视 SDK 下的未定义符号"__Unwind_SjLj_Unregister"
- 使用 Boost/Python 的未定义符号 - 复杂
- 建筑x86_64的未定义符号:"vtkDebugLeaksManager::vtkDebugLeaksManager()",引用自: