0
的静态成员看一看下面的代码:麻烦访问类
//abc.h
class abc{ public: static int a; void init(); };
//abc.cpp
#include"abc.h"
int abc::a;
void abc::init(){
abc::a = 10;
}
//主.cpp
#include<iostream>
#include"abc.h"
int main(){
std::cout << abc::a;
return 0;
}
基本上我想实现的是一类写入一个静态变量,另一个类从它读取。写操作发生在每次阅读之前。我收到以下错误:
[email protected]:~/test$ g++ main.cpp
/tmp/ccREguak.o:main.cpp:function main: error: undefined reference to 'abc::a'
collect2: error: ld returned 1 exit status
请帮助我究竟做错了什么。
你没有链接文件abc.cpp。将它添加到你的编译命令行。 – Mat
“写操作每次都在阅读之前发生” - 不在您发布的代码中。 – Mat
修复了这个问题...谢谢你 – Anvith