xcode 非 POD 元素类型“字符串”(又名“basic_string<char>”)的可变长度数组 c++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9550712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:31:21  来源:igfitidea点击:

Variable length array of non-POD element type 'string' (aka 'basic_string<char>') c++

c++arraysxcodestringxcode4

提问by bobthemac

I get this error in my c++ code Variable length array of non-POD element type string(aka basic_string<char>).

我在我的 C++ 代码非 POD 元素类型string(又名basic_string<char>)的可变长度数组中收到此错误。

string words[numWords];

If I get rid of numWords and enter a number this works fine but if i put the same number in a variable it gives me the Variable length array of non-POD element type 'string' (aka 'basic_string<char>')error, I have done it this way before and it worked in visual studio but I have now tried it in Xcode and it doesn't work. I have tried using vectors but I can't get them sot store any data and they just come back blank.

如果我摆脱 numWords 并输入一个数字,这可以正常工作,但是如果我将相同的数字放入变量中,则会Variable length array of non-POD element type 'string' (aka 'basic_string<char>')出现错误,我以前这样做过并且在 Visual Studio 中有效,但我现在已在 Xcode 中尝试过它不起作用。我试过使用向量,但我无法让它们存储任何数据,它们只是返回空白。

For those who asked this is my vector code should all be there

对于那些问这是我的矢量代码的人应该都在那里

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();

回答by Carl Norum

C++ doesn't have C99-style variable length arrays. Your compiler might support them as an extension, but they're not part of the language. In this specific case, your success with Visual Studio indicates that it does in fact have such an extension. clang++ will support VLAs, but only of POD types, so your attempt to make a VLA of stringobjects won't work. g++ does work on my machine if I leave off enough warning/error flags.

C++ 没有 C99 风格的可变长度数组。您的编译器可能支持它们作为扩展,但它们不是语言的一部分。在此特定情况下,您使用 Visual Studio 的成功表明它确实具有这样的扩展。clang++ 将支持 VLA,但仅支持 POD 类型,因此您尝试创建string对象的 VLA将不起作用。如果我留下足够的警告/错误标志,g++ 确实可以在我的机器上工作。

回答by hmjd

This initializes wordswith numWordsempty strings then appends the actual strings afterwards:

wordsnumWords空字符串初始化,然后附加实际字符串:

vector<string> words (numWords);

while(repFile >> x)
    words.push_back(x);

Change to:

改成:

vector<string> words;

while(repFile >> x)
    words.push_back(x);

or:

或者:

vector<string> words (numWords);

int idx = 0;
while(repFile >> x /* && idx < numWords */)
    words[idx++] = x;

EDIT:

编辑:

There is no reason to count the number of words before populating the vector:

在填充 之前没有理由计算单词的数量vector

vector<string> words;
ifstream repFile("//Users//bobthemac//Documents//c++asignment//c++asignment//test1.txt");
if (repFile.is_open())
{
    while(repFile >> x)
    {
        words.push_back(x);
    }
    repFile.close();
}

回答by Irfy

Sorry, you need to write gcc --versionto get the version.

抱歉,您需要编写gcc --version以获取版本。

As others state, you shouldn'tuse variable-length arrays, but GCC does support them as an extension in C++. My GCC 4.4.4 compiles just fine with the following code:

正如其他人所说,您不应该使用可变长度数组,但 GCC 确实支持它们作为 C++ 中的扩展。我的 GCC 4.4.4 使用以下代码编译得很好:

#include <string>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  string s[n];
  return 0;
}

Does that code compile for you? If it does, then you need to give us the smallest piece of code that fails.

该代码是否为您编译?如果是,那么您需要给我们提供失败的最小代码段。

The best solution, though, is to use vector<string>.

不过,最好的解决方案是使用vector<string>.