Originally Posted by
jimblumberg
Am I missing something? What is wrong with
getline() with the std::string instead of using the C-string version of getline()?
Jim
How weird. This is what I tried in Visual Studio:
Code:
/**
* hello-world.cpp:
*/
#include <iostream>
#include <ios>
#include <limits>
#include <string>
#include <sstream>
int main(int argc, char * argv[])
{
std::string stuff;
std::cout << "Please enter a string: ";
std::getline(std::cin, stuff, 100);
std::cout << "The stuff: " << stuff << std::endl << std::endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
return 0;
}
And the errors that I got:
Code:
1>------ Build started: Project: hello-world, Configuration: Debug Win32 ------1> hello-world.cpp
1>c:\documents\visual studio 2010\projects\hello-world\hello-world\hello-world.cpp(34): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1> c:\program files\microsoft visual studio 10.0\vc\include\string(479) : see declaration of 'std::getline'
1>c:\documents\visual studio 2010\projects\hello-world\hello-world\hello-world.cpp(34): error C2782: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : template parameter '_Elem' is ambiguous
1> c:\program files\microsoft visual studio 10.0\vc\include\string(468) : see declaration of 'std::getline'
1> could be 'int'
1> or 'char'
1>c:\documents\visual studio 2010\projects\hello-world\hello-world\hello-world.cpp(34): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1> c:\program files\microsoft visual studio 10.0\vc\include\string(448) : see declaration of 'std::getline'
1>c:\documents\visual studio 2010\projects\hello-world\hello-world\hello-world.cpp(34): error C2782: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : template parameter '_Elem' is ambiguous
1> c:\program files\microsoft visual studio 10.0\vc\include\string(395) : see declaration of 'std::getline'
1> could be 'int'
1> or 'char'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This... should work...
Maybe my VS install is borked.
[edit]
I tried the above code in GCC 4.5.3 (in cygwin), these are the errors that I got:
Code:
hello-world.cpp: In function ‘int main(int, char**)’:
hello-world.cpp:14:38: error: no matching function for call to ‘getline(std::istream&, std::string&, int)’