std::cin working in Eclipse but not Visual Studio?

closed account (SzUk92yv)
Background info: I'm using Parallels Desktop 10 to run Windows 8.1 as a VM directly alongside of OS X 10.10. So on OS X, I primarily use Eclipse, and for Windows, I use Visual Studio.

I'm having a problem with std::cin working in Visual Studio. I'm not exactly sure what the problem is, it's just giving an error on the ">>" operator.

Example (underlined text is the error underlined in the IDE):

Eclipse:
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main(void) {
	string text;
	cout << "Enter some text: ";
	cin >> text;
	cout << "Hello, " << text << endl;
}


Visual Studio:
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main(void) {
	string text;
	cout << "Enter some text: ";
	cin >> text;
	cout << "Hello, " << text << endl;
}


And here's the error that Visual Studio is giving me:
"Error: no operator ">>" matches these operands
operand types are: std::istream >> std::string"

P.S. I'm pretty new to C++ programming, so forgive me if the answer is literally right there in front of me. But I think I just realized I should replace std::cin in my code with std::istream, according to the error that I'm just now understanding, lol.

EDIT: I just tried replacing std::cin with std::istream and it didn't work.
Last edited on
You cannot use std::string unless you include the <string> header. The fact that it works in Eclipse is pure luck; somehow including just <iostream> happened to eventually include <string>, but this is not guaranteed.
Last edited on
Add #include <string> . You should always include all headers you are using.
closed account (SzUk92yv)
Thanks for the help, LB and MiiNiPaa!

Lol, I can't believe I didn't think to include the string header.
That's actually one of the skills that C++ programmers have to develop -- making sure to #include all the appropriate headers. Testing your program against multiple compilers helps a lot, but it is best just to keep a reference handy, so whenever you begin to use some class or function in your program you can make sure to add the proper #include to the top of the file.

I always (with minor exceptions for some POSIX stuff) sort my headers alphabetically.

For example:

1
2
#include <iostream>
#include <vector> 

I wish to use std::count_if(), so I add <algorithm> to the top:

1
2
3
#include <algorithm>
#include <iostream>
#include <vector> 

Now I wish to use some output manipulators (like std::setw()), so I include the <iomanip> header:

1
2
3
4
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector> 

I wish to use something OS-specific, (isatty()/GetConsoleMode()) so I include them specially in their own section:

1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <iostream>
#include <vector>

#ifdef _WIN32
  #ifndef NOMINMAX
  #define NOMINMAX
  #endif
  #include <windows.h>
#else
  #include <unistd.h>
#endif 

Etc.

When I'm ready to finish with a file, I again look through the headers to see if I've included any I don't really need after removing or changing some code that needed it before.

Hope this helps.
The header situation in C++ is a known problem, and hopefully modules will alleviate many of the issues associated with headers. But, they're a ways off.
Topic archived. No new replies allowed.