2015-12-03 34 views
1

我有这样的一个下面添加一个整数一个struct

“不兼容的类型中的intint [10000]分配”

我不明白什么是错的错误。这里是我的代码:

#include<fstream> 
#include<string> 
#include<vector> 
#include<algorithm> 

using namespace std; 

struct words{ 

    string lexis; 
    int sizes[10000]; 

} a[10000]; 

bool s(const words& a,const words& b); 

//========================================== 
int main() { 
    int i,x; 
    string word; 

    //input-output with files 
    ifstream cin("wordin.txt"); 
    ofstream cout("wordout.txt"); 

    i = 0; 

    //reading until the end of the file 
    while(!cin.eof()){ 
     cin >> word; 

     x = word.size(); 

     a[i].sizes = x; //the problem is here 

     a[i].lexis = word; 
     i++; 
    } 

} 

我真的很感激,如果有人帮助我。 :) 感谢

+2

做**不**使用'cin.eof()'作为循环读取输入的主要条件。另外,使用'while(cin >> word){...}' –

+4

'a [i] .sizes'产生一个int(&)[10000]',检查任何输入_after_读和_before_。你可以给'a [i] .sizes [j]'分配一个'int'。 –

+2

你确定你想要10000个10000个数组的数组吗? IT看起来你的结构在使用方面没有很好的定义。不要在C++中使用原始数组,请使用更方便的向量(动态数组)。 –

回答

1

避免使用变量具有相同标准库的名字,你的情况重命名两种文件流cincout,例如,my_cinmy_cout

如果你想读取多个string S或int的使用std::vector代替阵列,即,而不是你的struct words你可以使用:

vector<string> words; 

,然后从文件中读取,你可以这样做:

// attach stream to file 
ifstream my_cin("wordin.txt"); 

// check if successfully opened 
if (!my_cin) cerr << "Can't open input file!\n"; 

// read file line by line 
string line; 
while (getline(my_cin, line)) { 

    // extract every word from a line 
    stringstream ss(line); 
    string word; 
    while (ss >> word) { 

     // save word in vector 
     words.push_back(word); 
    } 
}