c++ - 如何修复 “Undefined symbols for architecture x86_64:” C++编译器错误?

标签 c++ compiler-errors

我正在做学校作业。我有Wallet.h和Main.cpp
在Wallet.h中,我有一个公众:

static std::map<std::string, double> currencies;
Wallet();在Main.cpp中,我尝试使用以下货币映射:
std::map<std::string, double> currencies = wallet.currencies;`
我在代码中没有收到任何警告,但是当我尝试编译时出现此错误:

Undefined symbols for architecture x86_64: "Wallet::currencies", [...] ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)


我不确定发生了什么问题,可能是我尝试使用货币的方式不正确。
有人可以帮忙吗?

最佳答案

如果没有适当的minimal reproducible example,很难说出任何具体的东西,但是似乎

std::map<std::string, double> currencies = wallet.currencies;
您定义一个全局 -member变量,而不是Wallet成员。
您需要将其显式定义为Wallet类的成员:
std::map<std::string, double> Wallet::currencies = wallet.currencies;
//                            ^^^^^^^^
//    Note scope as Wallet member here

关于c++ - 如何修复 “Undefined symbols for architecture x86_64:” C++编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65479015/

相关文章:

c++ - 使用 invoke_result 的正确方法?

c++ - node-gyp 平台特定插件

c++ - Visual Studio C++ 多项目解决方案

c++ - 删除全局隐式函数 - 避免模棱两可的运算符

kotlin - 为什么不能声明作为密封类的子类的嵌套类?

c++ - uvec2 片段着色器输出的哪种组合

c++ - 为什么存在奇特的指针?

c++ - 错误 "cannot convert ' float '到 'float...' ..."

c - 错误 : case label does not reduce to an integer constant when using character data type

c - 尝试编译 TPC-H Benchmark 并返回此错误 ld : library not found for -lgcc