链接器命令失败,macOS 上的退出代码为 1(使用 -v 查看调用)
linker command failed with exit code 1 (use -v to see invocation) on macos
我正在练习一个简单的 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 ++调用其他模块中的函数?
- 使用 %s 调用 printf 并传递零长度的字符*是未定义的行为吗?
- 使用 "()" 调用构造函数不同于"{}"
- 使用 c++ 调用.dll的默认参数的函数
- 当我同时将 T& 和 T 作为同名函数的返回类型时,如何使用 T& 调用函数
- 使用 P/调用传递取消标志时是否需要同步
- 当被调用方法使用调用方已锁定的同一锁时,如何避免死锁
- 在QT Creator中使用C++调用Python脚本
- C++按引用传递:如何使用调用堆栈
- 如何使用调用和别名指令在 llvm 字节码中查找函数名称
- 如何使用调用和分配指令在 llvm 字节码中查找类名
- 使用C++调用带有库的powershell脚本
- c++-如何使用调用函数的对象
- 使用 C++ 调用函数,其名称两边带有括号
- 如何 使用 C++ 调用 LabVIEW 构建 的 DLL
- 使用c#调用返回struct数组的非托管dll函数
- CUDA c++:使用调用模板内核的模板函数
- 如何创建win32智能手机dll并在WinCE中使用c#调用dll
- 使用 "->" 调用函数
- 错误:使用 C++ 调用 没有匹配函数