Xcode未定义符号~析构函数

Xcode undefined symbols ~ destructor

本文关键字:析构函数 符号 未定义 Xcode      更新时间:2023-10-16
Undefined symbols for architecture x86_64:
  "Matrix::~Matrix()", referenced from:
      _main in p1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我得到这个错误时,试图在xcode编译。问题在于头文件中的析构函数声明。如果我把析构函数注释掉,它就会成功构建。

class Matrix {
public:
    ~Matrix();     
}; 

您还没有定义析构函数。当注释掉它时,析构函数将是隐式声明的析构函数,因此链接器会找到它。你说"……将被定义"。这是否意味着你还没有定义它?

From cppreference.com (http://en.cppreference.com/w/cpp/language/destructor)

如果没有为类类型(struct)提供用户声明的析构函数,类或联合),编译器总是将析构函数声明为类的内联公共成员。