【发布时间】:2020-05-01 09:11:41
【问题描述】:
我试图从另一个类中声明的数组中获取一些值。该数组具有固定长度和常量元素(我将 100% 永远不会修改其值,所以这就是我将其设为常量的原因)。
但是,当我尝试访问 main 函数中的第一个元素时,出现编译错误:
basavyr@Roberts-MacBook-Pro src % g++ -std=c++11 main.cc
Undefined symbols for architecture x86_64:
"Vectors::vec1", referenced from:
_main in main-c29f22.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
如您所见,我正在使用 clang(最新版本)在 macOS Catalina 上进行编译。
[Q]:可能是什么问题? 提前谢谢你。
代码如下:
#include <iostream>
class Dimension
{
public:
static constexpr int dim1 = 2;
static constexpr int dim2 = 1;
};
class Vectors
{
public:
static constexpr double vec1[2] = {4.20, 6.9};
};
int main()
{
auto a = Vectors::vec1[0]; //I also tried initializing this to a value rather than just accessing it directly through the class like I did below
std::cout << a << "\n";
std::cout << Vectors::vec1[0] << "\n";
return 0;
}
【问题讨论】:
-
似乎与wandbox 上的最新clang 配合得很好。