這裏是我有什麼(不工作)的簡化版本:如何在C++中使用常量數組的成員函數?
prog.h:
...
const string c_strExample1 = "ex1";
const string c_strExample2 = "ex2";
const string c_astrExamples[] = {c_strExample1, c_strExample2};
...
prog.cpp:
...
int main()
{
int nLength = c_astrExamples.length();
for (int i = 0; i < nLength; i++)
cout << c_astrExamples[i] << "\n";
return 0;
}
...
當我嘗試建立,我得到以下錯誤: 錯誤C2228:'.length'的左邊必須有class/struct/union
只有當我嘗試使用成員func c_astrExamples的部分。 如果我用數字2替換「c_astrExamples.length()」,一切似乎都正常工作。
我能夠使用c_strExample1和c_strExample2的成員函數,所以我認爲行爲出現在我使用字符串與字符串數組之間的一些區別。
我在prog.h中的初始化是否錯誤?我需要prog.cpp中特別的東西嗎?
@Grumdrig:你需要開始一個新的句子:」 .. .vector。如果你......' –
quamrana
2009-10-28 19:12:39
@quamrana - Grumdrig的句子正確解析我。 「如果你想要,你應該使用X,否則就這樣做。」完美的語法結構。 – asveikau 2009-10-28 19:15:41
我還會注意到提問者比他小心sizeof()。它經常混淆新手,它不會告訴你編譯器不知道的任何東西的大小,例如。動態分配的內存,或作爲參數傳遞給函數的東西。 – asveikau 2009-10-28 19:21:03