简体   繁体   English

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

[英]linker command failed with exit code 1 (use -v to see invocation) on macos

I am practicing a simple c++ code base on example我正在练习基于示例的简单 C++ 代码

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

};

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

but when I use g++ to compile this code it occur this error但是当我使用 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)

Can anybody tell me what's wrong?谁能告诉我出了什么问题?

here is my g++ version:这是我的 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

The problem is that you have not provided a definition of the constructor.问题是您没有提供构造函数的定义。

This function is implicitly calling Tset::Tset() , the constructor:此函数隐式调用Tset::Tset() ,构造函数:

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

But in your class, you do not define the constructor:但是在您的类中,您没有定义构造函数:

class Tset {
public:
    Tset();            // No definition.  :-(
    Tset operator+(const Tset& a);
};

You can fix this by providing a definition of the constructor, or defaulting the constructor:您可以通过提供构造函数的定义或默认构造函数来解决此问题:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xcode链接器错误:链接器命令失败,退出代码为1(使用-v查看调用) - Xcode linker error: linker command failed with exit code 1 (use -v to see invocation) Netbeans 10:错误:链接器命令失败,退出代码为1(使用-v查看调用) - Netbeans 10: error: linker command failed with exit code 1 (use -v to see invocation) clang:错误:在cmake中链接库时,链接器命令失败,退出代码为1(使用-v查看调用) - clang: error: linker command failed with exit code 1 (use -v to see invocation) when linking library in cmake 如何解决错误:链接器命令失败,退出代码为1(使用-v查看调用)? - How to fix error: linker command failed with exit code 1 (use -v to see invocation)? 铛:错误:链接器命令失败,退出代码为1(使用-v查看调用)*关于全局变量 - clang: error: linker command failed with exit code 1 (use -v to see invocation) *about global variables VSCode:clang:错误:linker 命令失败,退出代码为 1(使用 -v 查看调用) - VSCode: clang: error: linker command failed with exit code 1 (use -v to see invocation) clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)控制台应用程序 - clang: error: linker command failed with exit code 1 (use -v to see invocation) console app 铛:错误:链接器命令失败,退出代码为1(使用-v查看调用)MINIX3 - clang: error: linker command failed with exit code 1 (use -v to see invocation) MINIX3 Mac上的C ++:链接器命令失败,退出代码为1(使用-v查看调用) - C++ on mac : linker command failed with exit code 1 (use -v to see invocation) Xcode:链接器命令失败,退出代码为1(使用-v查看调用)[C ++] - Xcode: linker command failed with exit code 1 (use -v to see invocation) [C++]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM