我在名爲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