我在链接代码时收到如下所示的错误。我该如何解决这个问题?
静态变量似乎没有被初始化。
#include <iostream>
#include <cstdlib>
using namespace std;
struct name{
char c;
};
class List {
static name *a;
public:
static void modify()
{
a = new name();
cout<<"yes";
}
};
name List::*a = NULL;
int main()
{
List::modify();
}
g++ O3 -Wall -c -fmessage-length=0 -o sample.o "..\\sample.cpp"
g++ -o sample.exe sample.o
sample.o:sample.cpp:(.text.startup+0x35): undefined reference to `List::a'
collect2.exe: error: ld returned 1 exit status
最佳答案
name List::*a = NULL;
没有达到您的预期。它定义了一个名为a
的全局变量,它是一个指向List
类型为name
的非静态成员的指针。
List::a
的定义应该是
name* List::a = NULL;
关于c++ - 错误消息 "undefined reference to ' 列表::a'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55219747/