Thread: Mismatching parameters

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    173

    Mismatching parameters

    I'm having a small problem with "matching up" the function definition/prototype with the function definition. I've stared at the code for ages already and can see no clue in where I'm at fault. The compiler error doesn't help much either since I'm just learning C++. Here's my current code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <map>
    #include <string>
    #include <stdexcept>
    
    void mapWords(std::multimap<std::string, int> &wordList, std::ifstream &inputFile);
    void mapOutput(std::multimap<std::string, int> &wordList, std::string word);
    
    void mapWords(std::multimap<std::string, int> &wordList, std::ifstream &inputFile)
    {
    	std::string word;
    	std::string line;
    
    	int linenum = 1;
    
    	while(std::getline(inputFile, line)) {
    		// deal with the words
    		std::istringstream inStream(line);
    		while (inStream >> word) {
    			wordList.insert(std::make_pair(word,linenum));
    		}
    
    		linenum++;
    	}
    }
    
    int main(int argc, char **argv)
    {
    	std::ifstream inFile;
    	std::multimap<std::string, int> mapWords;
    
    	if (argc != 2) {
    		std::cerr << "Needs exactly 2 arguments." << std::endl;
    		throw std::runtime_error("incorrect argument usage");
    	}
    	
    	inFile.open(argv[1]);
    	if (!inFile) {
    		std::cerr << "Could not open text file." << std::endl;
    		throw std::runtime_error("error opening input");
    	}
    	
    	mapWords(inFile,mapWords);
    	
    	std::cout << "Query for a word: " << std::endl;
    	std::string word;
    	
    	std::cin.clear();
    	std::cin >> word;
    
    	mapOutput(mapWords,word);
    	return 0;
    }	
    
    void mapOutput(std::multimap<std::string, int> &wordList, std::string word)
    {
    	typedef std::multimap<std::string, int>::size_type sztype;
    	sztype cnt = wordList.count(word);	
    	
    	std::cout << "element occurs " << cnt << " times" << std::endl;
    	
    	// now print each instance		
    	typedef std::multimap<std::string, int>::iterator word_iter;	
    	std::pair<word_iter, word_iter> pos = wordList.equal_range(word);
    	while (pos.first != pos.second) {
    		std::cout << "\t(line " << pos.first->second << ")" << std::endl;
    		pos.first++;
    	}
    	
    	// output purposes
    	std::cout << std::endl;
    }
    The problem lies specifically in the mapWords() function, the compiler gives me the following error:
    Code:
    textquery.cpp: In function `int main(int, char**)':
    textquery.cpp:45: error: no match for call to `(std::multimap<std::string, int, 
       std::less<std::string>, std::allocator<std::pair<const std::string, int> > >
       ) (std::ifstream&, std::multimap<std::string, int, std::less<std::string>, 
       std::allocator<std::pair<const std::string, int> > >&)'
    Which isn't helpful but it just tells me that the function calls don't match properly
    Am I missing something obvious here? Or does it lie on how the maps/multimaps are used in parameters? Any pointers would be greatly appreciated
    The cost of software maintenance increases with the square of the programmer's creativity.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The cost of software maintenance increases with the square of the programmer's creativity.
    Trying to use the same name for a variable and a function is a little too creative!
    Code:
    mapWords(inFile,mapWords);
    Your arguments are reversed.

  3. #3
    Registered User Code Monkey's Avatar
    Join Date
    Jan 2006
    Location
    Denton
    Posts
    11
    Just like anonytmouse said, your variable name for your multimap is the same as a function name.

    If you are declaring a function that is not a member of a class or struct you should use a naming convenation so that you know it's a global function and other people that read your code know its global.


    void g_MapWords(std::multimap<std::string, int> &wordList, std::ifstream &inputFile);
    void g_MapOutput(std::multimap<std::string, int> &wordList, std::string word);

    The "g_" pretty much says that hey this is a global function. Another part in your code, I think you should look at is:

    g_MapWords(inFile,mapWords); <- mapWords global function

    This function call has the parameters backwards, shouldn't it be:

    g_MapWords(mapWords, inFile);

    Because the parameters ask for the multimap then the file buffer.

    Edit: I hope this helps

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Ahhhh! I didn't see it coming!
    *hides in the shadows*

    I quickly coded it so I didn't pay much attention to the variable names as I just wanted to learn the maps! Thanks for the input.. guess I won't fall for the same mistake (hopefully) next time!
    The cost of software maintenance increases with the square of the programmer's creativity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-21-2008, 06:04 AM
  2. Parameters quick Question
    By lifeis2evil in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2007, 11:12 PM
  3. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  4. Additional parameters for operator delete
    By darksaidin in forum C++ Programming
    Replies: 0
    Last Post: 09-21-2003, 11:46 AM
  5. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM