【问题标题】:error: variable length array of non-POD element type 'string' (aka 'basic_string<char>')错误:非 POD 元素类型 'string' 的可变长度数组(又名 'basic_string<char>')
【发布时间】:2015-11-17 14:28:08
【问题描述】:

我知道最终我需要将一个空格包含 3 个字符的三元组从前一个字符串更改为一个动态数组来解决这个问题,但我一开始尝试将我的数组的容量设置得足够大。但是,当我编译我的代码时,就会出现错误。

#error: variable length array of non-POD element type 'string' (aka 'basic_string<char>'#

代码:

//global variable
int CAPACITY = 1000;

int main()
{
    //a string that reads in the language of the text
string language = "";
    //a string that reads in the file name of the text
string filename = "text.txt";
    //a string that reads in the original text characters
string original = "";
    //a string that reads in the modified original array
string rid_of_spaces = "";
    //an array with capacity that stores the trigrams
string trigrams[CAPACITY];
ifstream finput;
char c;
    //the length of an array
int sLength = 0;
    //the tracker for trigrams
int counter = 0;

cin >> language >> filename;
finput.open(filename.c_str());

while (finput.get(c)){
            //to test if the character is alpha
    if (isalpha(c)){
                    //change the alphabet to lowercase
        c = tolower(c);
                    //store the modified letter in the array
        original += c;
    }
            //change any other characters into a space
    else original += ' ';
}
sLength = original.length();

    //loop through the original array and change mutiple spaces into one 
for (int i = 0; i < sLength; i++){
    if (isalpha(original[i]))
        rid_of_spaces += original[i];
    else {
        while (original[i] == ' ')
            i++;
        rid_of_spaces += ' ';
        rid_of_spaces += original[i];
    }
}
sLength = rid_of_spaces.length();

for (int i = 0; i < CAPACITY; i++)
    trigrams[i] = 0;//initialize each element to 0

for (int i = 0; i < sLength - 2; i++){
    trigrams[counter] += rid_of_spaces[i] 
            + rid_of_spaces[i + 1]
            + rid_of_spaces[i + 2];
        counter++;
}

cout << filename << endl;

cout << original << endl;
cout << rid_of_spaces << endl;

for (int i = 0; i < counter; i++)
    cout << trigrams[i] << endl;

finput.close();
return 0;

}

【问题讨论】:

  • std::vector代替动态数组怎么样?
  • 试试const int CAPACITY
  • @MikeCAT 对不起,但不知道那是什么
  • std::vector 是 C++ 标准模板库之一,它提供了变长数组。
  • @MikeCAT 哦,谢谢 :)

标签: c++ arrays string compiler-errors dynamic-arrays


【解决方案1】:

变量

int CAPACITY = 1000;

应该是一个常数

const int CAPACITY = 1000; // or with c++11 constexpr int CAPACITY = 1000;  

string trigrams[CAPACITY];

因为“ISO C++ 禁止 可变 长度数组 'trigrams'”(g++ 消息)

还有这个

for (int i = 0; i < CAPACITY; i++)
    trigrams[i] = 0;//initialize each element to 0

应该是

for (int i = 0; i < CAPACITY; ++i)
    trigrams[i] = "";//initialize each element to 0

您不会“将 [字符串] 初始化为 0”,而是使用 零长度 C 字符串。零长度 C 字符串不是无效的 0 指针,而是指向值为 0 的 char 的(有效)指针;

一般来说,如果有 STL-means 来避免它们,最好不要使用 C 数组;使用 c++11,如果您想保持“容量足够大”的方法,std::array&lt;std::string, CAPACITY&gt; 在这里会更可取。

live 在 Coliru 的

我冒昧地将for-loops 中的所有i++ 更改为++i;见例如。 What is the difference between ++i and i++ 了解其背后的基本原理。

对于动态(没有预定义边界)数组使用std::vector&lt;std::string&gt; trigrams;

push_backemplace_back 将您的字符串放入该向量中, 和迭代

for (std::size_t i = 0; i < trigrams.size(); ++i) {/* ... */}

或者使用std::vector的迭代器接口,例如

std::for_each(trigrams.begin(), trigrams.end(), 
              some_function_or_functor_that_does_the_job); 

(见 std::foreach here),

或者只用 c++11

for (auto& s : trigrams) {/* ... */}

除非您需要像在第二个循环中那样自定义迭代。

【讨论】:

    【解决方案2】:

    变量CAPACITY 不是编译时常量变量,C++ 中也没有可变长度数组(虽然有些将它作为扩展,但显然并非适用于所有类型)。

    您的问题有两种解决方案:

    1. 将变量转换为编译时常量,可以将其设为constexprconst(对于较旧的编译器),或者将其定义为预处理器宏。

    2. 使用std::vector,如std::vector&lt;std::string&gt; trigram(CAPACITY);

    我的建议是您使用上述 both 解决方案,至少在您以后需要调整矢量大小时使用。如果大小将在编译时固定且永不更改,则使用第一个解决方案并且使用std::array 而不是 C 样式数组:

    constexpr std::size_t CAPACITY = 1000;
    
    ...
    
    std::array<std::string, CAPACITY> trigram;
    

    【讨论】:

      【解决方案3】:

      C++ 数组的大小必须是常量表达式。您将其声明为int CAPACITY = 1000;,这不是一个常量表达式。添加const 限定符可解决问题:int const CAPACITY = 1000;

      但是,您应该避免使用纯数组。如果您在编译时知道大小,请尝试使用std::array,否则请尝试使用std::vector

      【讨论】:

        猜你喜欢
        • 2012-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-30
        • 2015-07-27
        • 1970-01-01
        • 2023-01-25
        相关资源
        最近更新 更多