By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,223 Members | 1,403 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,223 IT Pros & Developers. It's quick & easy.

Getline Compile Error C2665

Schwack
P: 2
I've just started learning C++ (got bored at work) and I'm using VC++ to compile some simple code but I get a compile error in test.cpp when using "getline". I've searched the internet and this forum but can't find an answer. I hope that someone can help :-)

test.h:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class ReadFile
  8. {
  9. public:
  10.     //Constructor
  11.     ReadFile(string file_path = "\0") : text_file (file_path.c_str())
  12.     {
  13.         count = 0;
  14.     };
  15.  
  16.     //Destructor
  17.     ~ReadFile()
  18.     {
  19.         text_file.close();
  20.     };
  21.  
  22.     long count;
  23.     string line();
  24.     bool end_of_file();
  25.  
  26. protected:
  27.     ifstream text_file;
  28.     string next_line;
  29.  
  30. };
test.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include "test.h"
  2.  
  3. using namespace std;
  4.  
  5. string ReadFile::line()
  6.  
  7.     {
  8.         if (! text_file.eof())
  9.             {
  10.                 count += 1;
  11.             }
  12.  
  13.         ifstream::getline(text_file,next_line); // Get a line from the text file 
  14.         return next_line;
  15.     }
  16.  
  17. bool ReadFile::end_of_file()
  18.     {
  19.         return text_file.eof(); // Check if it's the end of the file
  20.     }
Build output:
------ Build started: Project: clr_project, Configuration: Debug Win32 ------
Compiling...
test.cpp
.\test.cpp(13) : error C2665: 'std::basic_istream<_Elem,_Traits>::getline' : none of the 2 overloads could convert all the argument types
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(593): could be 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_El em *,std::streamsize)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
while trying to match the argument list '(std::ifstream, std::string)'
Build log was saved at "file://c:\Documents and Settings\User\Desktop\clr_project\clr_project\Debu g\BuildLog.htm"
clr_project - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Jan 14 '08 #1
Share this Question
Share on Google+
2 Replies


Schwack
P: 2
Ok, I figured it out - really dumb typo in line 13, test.cpp

Expand|Select|Wrap|Line Numbers
  1. ifstream::getline(text_file,next_line);
All I had to do was remove "ifstream::" so tha the line read:

Expand|Select|Wrap|Line Numbers
  1. getline(text_file,next_line);
I can't believe how long it took for me to spot that!!!


I've just started learning C++ (got bored at work) and I'm using VC++ to compile some simple code but I get a compile error in test.cpp when using "getline". I've searched the internet and this forum but can't find an answer. I hope that someone can help :-)

test.h:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class ReadFile
  8. {
  9. public:
  10.     //Constructor
  11.     ReadFile(string file_path = "\0") : text_file (file_path.c_str())
  12.     {
  13.         count = 0;
  14.     };
  15.  
  16.     //Destructor
  17.     ~ReadFile()
  18.     {
  19.         text_file.close();
  20.     };
  21.  
  22.     long count;
  23.     string line();
  24.     bool end_of_file();
  25.  
  26. protected:
  27.     ifstream text_file;
  28.     string next_line;
  29.  
  30. };
test.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include "test.h"
  2.  
  3. using namespace std;
  4.  
  5. string ReadFile::line()
  6.  
  7.     {
  8.         if (! text_file.eof())
  9.             {
  10.                 count += 1;
  11.             }
  12.  
  13.         ifstream::getline(text_file,next_line); // Get a line from the text file 
  14.         return next_line;
  15.     }
  16.  
  17. bool ReadFile::end_of_file()
  18.     {
  19.         return text_file.eof(); // Check if it's the end of the file
  20.     }
Build output:
------ Build started: Project: clr_project, Configuration: Debug Win32 ------
Compiling...
test.cpp
.\test.cpp(13) : error C2665: 'std::basic_istream<_Elem,_Traits>::getline' : none of the 2 overloads could convert all the argument types
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(593): could be 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_El em *,std::streamsize)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
while trying to match the argument list '(std::ifstream, std::string)'
Build log was saved at "file://c:\Documents and Settings\User\Desktop\clr_project\clr_project\Debu g\BuildLog.htm"
clr_project - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Jan 14 '08 #2

sicarie
Expert Mod 2.5K+
P: 4,677
Thanks for posting your resolution, sorry we couldn't help.
Jan 14 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.