I am having problems getting this code to run correctly:
Code:
// wordstat.cpp
// STL version of word statistics utility
// The program parses an input text file
// and assigns the words to a std::list.
// Sort the list in alphabetical order and remove
// duplicate items.
// Operator== is overloaded to determine duplicate-ness
// of two items. If a duplicate item is found,
// the word counter gets increased.
//
// Feb. 25, 2001 - accepts cin from console as well as a file.
//
#include <list>
#include <string>
#include <iostream>
#include <fstream>
class node
{
public:
node(std::string& s) : word(s), count(1) {}; // ctor
bool operator<(node const& n) const {
return (word < n.word);
};
bool operator==(node& n) {
if(word == n.word) {
this->count++; // duplicate found. Increase counter.
return true;
}
else
return false;
};
friend std::ostream& operator<<(std::ostream& os, const node& n);
private:
std::string word;
int count; /* word counter */
};
std::ostream& operator<<(std::ostream& os, const node& n)
{
os << n.count << "\t" << n.word;
return os;
}
// retrieve text from an input stream
// and build words list.
bool retrieve_text(std::list<node>& words, // words list
std::istream &is, // stream to open
const std::string& delim) // delimiter; used to parse text
{
std::string textline;
while(is >> textline) {
std::string::size_type pos = 0, pos_end;
if((pos = textline.find_first_not_of(delim, pos)) != std::string::npos) {
while((pos_end = textline.find_first_of(delim, pos)) != std::string::npos)
{
if( pos != pos_end)
words.push_back(textline.substr(pos, pos_end - pos));
pos = textline.find_first_not_of(delim, pos_end);
}
if( pos != pos_end)
words.push_back(textline.substr(pos, pos_end - pos));
}
}
return true;
}
int main(int argc, char **argv)
{
const std::string delim = " \t\n\r.,;:()\"";
std::list<node> words;
if(argc >= 2) {
std::ifstream file;
file.open(argv[1]);
if(file)
retrieve_text(words, file, delim);
else{
std::cerr<< "File not found.\n";
exit(-1);
}
}
else{
retrieve_text(words, std::cin, delim);
}
words.sort();
words.unique(); // remove duplicates
// display word statistics
std::list<node>::iterator iter=words.begin(), iter_end = words.end();
for(; iter != iter_end; ++iter)
std::cout << (*iter) << std::endl;
return 0;
}
these are the problems the debugger is giving me but i cant figure them out because i am relatively new to the c language.
line 66 no matching function for call to `std::list<node, std::allocator<node> >:: push_back(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
line 70 no matching function for call to `std::list<node, std::allocator<node> >:: push_back(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Any help is greatly appreciated