2015-11-17 18 views
2

我知道最终我需要将trigram,其中一个空格包含来自前一个字符串的3个字符,更改为一个动态数组来解决这个问题,但我试图首先设置我的阵列容量足够大。但是,当我编译我的代码时,会显示错误。错误:非POD元素类型“字符串”(又名'basic_string <char>')的可变长度数组

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

}

+0

如何使用'std :: vector'而不是动态数组? – MikeCAT

+1

尝试'const int CAPACITY' – user463035818

+0

@MikeCAT对不起,但不知道是什么 –

回答

1

可变

int CAPACITY = 1000; 

应恒定

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

string trigrams[CAPACITY]; 

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

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的字符的(有效)指针;

通常,最好不要使用C数组,如果有STL方法来避免它们的话;与c + + 11,std::array<std::string, CAPACITY>将在这里更好,如果你想留在“容量足够大”的方法。

live在Coliru的

我冒昧所有i++改变在for -loops的头++i而在它;见例如。 What is the difference between ++i and i++为背后的理由。

对于动态(没有预先定义的边界)阵列使用std::vector<std::string> 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) {/* ... */} 

,除非你需要像你这样做你的第二个循环中自定义迭代。

0

一个C的大小++阵列必须是一个常量表达式。你声明它为int CAPACITY = 1000;,这不是一个常量表达式。添加const限定符可解决此问题:int const CAPACITY = 1000;

但是,你应该避免普通数组。如果您知道编译时的大小,请尝试使用std::array,如果不知道,则使用std::vector

1

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

有两个解决问题的方法:

  1. 打开可变进编译时间常数,无论是通过使constexpr或者const(用于较老的编译器),或者将其定义为一个预处理宏。

  2. 使用std::vector,像std::vector<std::string> trigram(CAPACITY);

我的建议是,你使用上面解决方案,至少如果以后需要调整的载体。如果尺寸将被固定在编译时和不能改变的,然后使用第一溶液使用std::array代替C数组:

constexpr std::size_t CAPACITY = 1000; 

... 

std::array<std::string, CAPACITY> trigram; 
相关问题