【发布时间】:2012-03-21 23:37:19
【问题描述】:
我在我的 c++ 代码中收到此错误,非 POD 元素类型的可变长度数组string(又名basic_string<char>)。
string words[numWords];
如果我摆脱 numWords 并输入一个数字,这很好,但如果我将相同的数字放在一个变量中,它会给我 Variable length array of non-POD element type 'string' (aka 'basic_string<char>') 错误,我以前这样做过,它在 Visual Studio 中工作,但我现在已经在 Xcode 中尝试过了,但它不起作用。我尝试过使用向量,但我无法让它们存储任何数据,它们只是返回空白。
对于那些问这是我的矢量代码的人应该都在那里
char ch;
ifstream repFile("//Users//bobthemac//Documents//c++asignment//c++asignment//test1.txt");
while(repFile.get(ch))
{
if(ch == ' ' || ch == '\n' || ch == '\t')
{
numWords++;
}
}
vector<string> words (numWords);
while(repFile >> x)
words.push_back(x);
repFile.close();
【问题讨论】:
-
这与 Xcode 无关。它与您使用的编译器有关。我认为某些版本的 gcc?
gcc -version说什么? -
你能发布你的
vector代码吗? -
对数组大小使用变量是不标准的。使用 vector
应该可以工作,所以真正的问题是找出为什么它不适合你。 -
gcc -version 说没有输入文件。
标签: c++ arrays xcode string xcode4