【发布时间】:2009-10-28 19:04:09
【问题描述】:
这是我所拥有的(不工作)的简化版本:
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' 左侧必须有类/结构/联合
该错误仅在我尝试使用 c_astrExamples 的成员函数时发生。 如果我用数字 2 替换“c_astrExamples.length()”,一切似乎都正常工作。
我能够使用 c_strExample1 和 c_strExample2 的成员函数,所以我认为这种行为是由于我使用字符串与字符串数组之间存在一些差异。
我在 prog.h 中的初始化是否错误?我需要 prog.cpp 中的特殊内容吗?
【问题讨论】: