我在名为GTAODV
的类中创建了一个静态成员数组。C++中的静态成员数组
gtaodv/gtaodv.o: In function `GTAODV::command(int, char const* const*)':
gtaodv.cc:(.text+0xbe): undefined reference to `GTAODV::numdetections'
gtaodv.cc:(.text+0xcc): undefined reference to `GTAODV::numdetections'
gtaodv/gtaodv.o: In function `GTAODV::check_malicious(GTAODV_Neighbor*)':
gtaodv.cc:(.text+0x326c): undefined reference to `GTAODV::numdetections'
gtaodv.cc:(.text+0x3276): undefined reference to `GTAODV::numdetections'
collect2: ld returned 1 exit status
为什么会这样:
static int numdetections[MAXNODES];
然而,当我试图将类的方法(下面的例子)中访问这个数组,
numdetections[nb->nb_addr]++;
for(int i=0; i<MAXNODES; i++) if (numdetections[i] != 0) printf("Number of detections of %d = %d\n", i, numdetections[i]);
链接器在编译过程中给出了一个错误发生?
因为你的链接器不知道'numdetections'的定义。你在哪里使用这个变量,定义在哪里? – 2012-04-22 16:52:31
我已经在类GTAODV中定义了numdetections,并且我在GTOADV成员函数中使用它。 – vigs1990 2012-04-22 16:53:57
请发布代码... – 2012-04-22 16:54:27