gpt4 book ai didi

c++ - 链接器命令在macOS上失败,退出代码为1(使用-v查看调用)

转载 作者:行者123 更新时间:2023-12-02 10:36:39 25 4
gpt4 key购买 nike

我正在基于示例练习一个简单的C++代码

class Tset{
public:
Tset();
Tset operator+( const Tset& a);

};

Tset Tset::operator+(const Tset& a){
Tset t;
return t;
}


但是当我用g++编译这段代码时
发生这个错误
Mac Desktop % g++ hw2.cpp
Undefined symbols for architecture x86_64:
"Tset::Tset()", referenced from:
Tset::operator+(Tset const&) in hw2-43d108.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

谁能告诉我怎么了?

这是我的g++版本:
Mac Desktop % g++ -v     
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.12)
Target: x86_64-apple-darwin19.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

最佳答案

问题是您尚未提供构造函数的定义。

此函数隐式调用构造函数Tset::Tset():

Tset Tset::operator+(const Tset& a){
Tset t; // Requires definition of constructor.
return t;
}

但是在您的类中,您没有定义构造函数:
class Tset {
public:
Tset(); // No definition. :-(
Tset operator+(const Tset& a);
};

您可以通过提供构造函数的定义或默认构造函数来解决此问题:
class Tset {
public:
Tset() = default;
Tset operator+( const Tset& a);
};

关于c++ - 链接器命令在macOS上失败,退出代码为1(使用-v查看调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59940429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com