读书人

C++list有关问题

发布时间: 2012-05-01 12:48:58 作者: rapoo

C++list问题
小弟新手 写了一段code 作用是用户input txt文件 系统分析txt文件里面出现多少单词 重复多少次 下面是code:
#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
#include <ctype.h>

#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

class wordList
{public:
int occuerence;
string word;

wordList(int occuerence1,string str):occuerence(occuerence1),word(str){};

~wordList();

void addOccuerence()
{occuerence += 1;}
};


void printList(list<wordList> &myList);

void openFile(list<wordList> &myList);


void printList(list<wordList> &myList)

{

list<wordList>::const_iterator itr;


for (itr = myList.begin(); itr != myList.end(); itr++ )
{
cout << ' ' << itr->word << "occurence " << itr->occuerence;
}

cout << '\n';

}



void openFile(list<wordList> &myList)

{
string filein;
ifstream fin;
char word[255];
char ch;
cout << "Please enter the file name: ";
cin >> filein;
fin.open(filein.c_str());
if (!fin){
cerr << "Can't open "<<filein
<< " \n";
fin.close();
exit(1);}
while(!fin.eof())
{
fin.get(ch);
switch(ch)
{
case ' ':
{list<wordList>::iterator itr;
for (itr = myList.begin(); itr != myList.end(); itr++ )
{if (word == itr->word)
itr->addOccuerence();
else
{string newword(word);
这里出错myList.push_back(new const wordList(1,newword));}}}
default:
{for(int i = 0;i < 255;i++)
{if(!word[i])
word[i] = ch;
break;}}}
}

fin.close();
}



int main ()
{

list<wordList> myList;


openFile(myList);

printList(myList);

return 0;
}

但是G++了之后出现这个错误:
no matching function for call to std::list(wordList,std::allocator(wordList))::push_back(const wordList*)
求解

[解决办法]
VC6.0编译运行都行啊;

C/C++ code
~wordList(){};//加个大括号
[解决办法]
myList.push_back(*new const wordList(1,newword));}}}

读书人网 >C++

热点推荐