小编Pri*_*ram的帖子

错误消息“未定义对'List :: a'的引用”

链接代码时出现以下错误。我该如何解决这个问题?

看来静态变量没有被初始化。

#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
Run Code Online (Sandbox Code Playgroud)

c++ static pointers

2
推荐指数
1
解决办法
72
查看次数

标签 统计

c++ ×1

pointers ×1

static ×1