gpt4 book ai didi

c++ - 错误 : variable length array of non-POD element type 'string' (aka 'basic_string' )

转载 作者:搜寻专家 更新时间:2023-10-31 02:19:14 24 4
gpt4 key购买 nike

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

#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;

最佳答案

变量

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

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

一般来说,如果有STL手段可以避免使用C数组,最好不要使用它们;使用 c++11,std::array<std::string, CAPACITY>如果您想继续使用“容量足够大”的方法,这里会更可取。

live在 Coliru 的

我冒昧地改了所有i++++ifor -loops 的头;见例如。 What is the difference between ++i and i++背后的理由。

对于动态(没有预定义边界)数组使用std::vector<std::string> trigrams; ,

push_backemplace_back你的字符串到那个 vector 中,对于 i- 迭代

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

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

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

(参见 std::foreach here ),

或仅使用 c++11

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

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

关于c++ - 错误 : variable length array of non-POD element type 'string' (aka 'basic_string<char>' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33759421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com