compiling issues..please help

This is a discussion on compiling issues..please help within the C++ Programming forums, part of the General Programming Boards category; I am having problems getting this code to run correctly: Code: // wordstat.cpp // STL version of word statistics utility ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    compiling issues..please help

    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

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Perhaps something like this:
    Code:
                if ( pos != pos_end )
                {
                   std::string sub(textline.substr(pos, pos_end - pos));
                   words.push_back(sub);
                }
    It has to do with reference/value semantics.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    tried that, didnt work... thanks tho

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems that making the constructor of node take a constant string fixes the problem.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    anon could u show me how to do that, i am very new with the c language

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Put the word const in front of string& s in the parameter list of the node constructor. Since you aren't modifying s, it can be const.

    It needs to be const because the return value of substr is just a temporary string, and you are trying to convert that temporary string to a node. Without the const, the node class only allows modifiable strings to be used with its constructor. It makes no sense to modify a temporary object (it will be destroyed immediately anyway) so the compiler makes you guarantee that you won't be modifying it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem compiling files that store functions
    By tiachopvutru in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2008, 05:42 PM
  2. Compiling Issues
    By pc_doctor in forum C Programming
    Replies: 3
    Last Post: 11-30-2007, 09:00 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problem Compiling
    By Flakster in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 01:09 AM
  5. Compiling
    By Dae in forum C++ Programming
    Replies: 7
    Last Post: 06-15-2005, 01:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21