1

I am trying to take user input directly from console using getline into a C++ string object. I am however failing to do so as the compiler gives me the following error.

main.cpp: In function ‘int main(int, char**)’:
main.cpp:52:28: error: no matching function for call to ‘std::basic_ifstream<char>::getline(std::ifstream&, std::string&)’
main.cpp:52:28: note: candidates are:
/usr/include/c++/4.6/istream:599:5: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize, std::basic_istream<_CharT, _Traits>::char_type) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::char_type = char, std::streamsize = int]
/usr/include/c++/4.6/istream:599:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/4.6/istream:408:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::char_type = char, std::streamsize = int]
/usr/include/c++/4.6/istream:408:7: note:   no known conversion for argument 2 from ‘std::string {aka std::basic_string<char>}’ to ‘int’

Here is the source code:

#include <iostream>
 #include <fstream>
 #include <cmath>
 #include <cstdlib>
 #include <cstring>
 #include <string>
 #include <map>
 #include <set>
 #include <algorithm>
 #include <cstdio>

 #define MAX_LIN 1024

 using namespace std;

 typedef set<int> si;
int main(int argc, char** argv)
 {   
     string line1;
     string line2;

     map <string , si > iMap;

     if (argc != 3) { 
         fprintf(stderr,"Usage: a.out <file1> <file2>\n");
     }

     ifstream f1,f2;

     f1.open(argv[1],ifstream::in);
     f2.open(argv[2],ifstream::in);

     while(!f1.eof()) {
         getline(f1,line1);
         mParse(line1);
     }   

     while(!f2.eof()) {
         f2.getline(f2,line2);
         mParse(line2);
     }   

     f1.close();
     f2.close();


     return 0;
 }   
4
  • Did you #include <string>? Please add your include directives to the posted code, I don't see any. Commented Oct 6, 2012 at 12:59
  • Have you done #include<fstream> in the header? Commented Oct 6, 2012 at 12:59
  • Yes, I have done both. Let me include the entire headers. Commented Oct 6, 2012 at 13:00
  • @humanitarian0098: It is a good idea to learn to read compiler messages. They are not always the prettiest, but we do have to deal with them on a daily basis after all. std::basic_ifstream<char>::getline(std::ifstream&, std::string&) can also be seen as std::ifstream::getline(std::ifstream&, std::string&), this is the compiler guessing what you were trying to call. Note how it indicates both that: 1/ it is a method of std::ifstream that you were calling and 2/ you passed std::ifstream& and std::string& as parameters. Commented Oct 6, 2012 at 14:27

2 Answers 2

6

This is the cause:

f2.getline(f2,line2);

should be:

getline(f2,line2);

There is a std::ifstream::getline() but it accepts different arguments.

Note the structure of the while loops is incorrect as eof() should be checked immediately after a read operation. A common way of coding the loop is:

while (getline(f2, line2))
{
    mParse(line2);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here's the canonical way:

 std::ifstream f1(argv[1]);
 std::ifstream f2(argv[2]);

 for (std::string line; std::getline(f1, line); )
 {
     mparse(line);
 }

 for (std::string line; std::getline(f2, line); )
 {
     mparse(line);
 }

Note everything we don't write, each of which should be considered a capital sin: eof, open, close, ifstream::in, return 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.