bob*_*mac 1 c++ arrays string xcode xcode4
我在我的c ++代码中得到此错误非POD元素类型string(aka basic_string<char>)的可变长度数组.
string words[numWords];
Run Code Online (Sandbox Code Playgroud)
如果我摆脱numWords并输入一个数字,这工作正常,但如果我把相同的数字放在一个变量,它给我Variable length array of non-POD element type 'string' (aka 'basic_string<char>')错误,我已经这样做过,它在视觉工作室工作,但我现在在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();
Run Code Online (Sandbox Code Playgroud)
C++没有C99样式的可变长度数组.您的编译器可能支持它们作为扩展,但它们不是该语言的一部分.在这种特定情况下,您使用Visual Studio的成功表明它确实具有这样的扩展.clang ++将支持VLA,但仅支持POD类型,因此您尝试制作string对象的VLA 将无法工作.如果我留下足够的警告/错误标志,g ++可以在我的机器上运行.