[英]Unresolved External Array
I am working on a school project and I am running into a linker error of 'unresolved external symbol' I have two files; 我在一个学校项目上工作,遇到“未解析的外部符号”的链接器错误,我有两个文件; math.h and math.cpp. math.h和math.cpp。 I am trying to declare some arrays in the namespace in math.h, but I am getting linker error from this. 我试图在math.h的命名空间中声明一些数组,但是我从中得到链接器错误。 In my math.h: 在我的math.h中:
namespace domath
{
extern float alpha1[];
extern Vector ecth[];
}
In my math.cpp: 在我的math.cpp中:
#include math.h
float alpha1[];
Vector ecth[];
What can I do to fix? 我该怎么办? I cannot assign a value like alpha1[] = {1}, because I do not know the value it will have. 我无法分配类似alpha1 [] = {1}的值,因为我不知道它将具有的值。 Thank very much in advance!! 提前非常感谢!
You declare the arrays in the domath
namespace, but define them in the global namespace. 您在domath
命名空间中声明数组,但在全局命名空间中定义它们。 Try this instead: 尝试以下方法:
// math.cpp
#include math.h
namespace domath
{
float alpha1[];
Vector ecth[];
}
The other reason it doesn't work is that you don't specify the size of the arrays. 另一个不起作用的原因是您没有指定数组的大小。 Either give them a fixed size up front, or declare them as pointers instead, or – typically best in C++ – use std::vector
. 要么预先给它们一个固定的大小,要么将它们声明为指针,或者-通常在C ++中最好-使用std::vector
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.