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