问题
I don't understand why this isn't working. For some reason I'm getting the error:
error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
I'm doing this in Visual Studio2010 C++ Express if that helps. Not sure why its handing me this error I've done other programs using cin
...
My Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main(int argc, char* argv){
string file;
if (argc > 1)
{
file = argv[1];
}
else
{
cout << "Please Enter Your Filename: ";
cin >> file;
}
}
回答1:
include <string>
On top of that I suggest you use getline instead as >> will stop at the first word in your input.
Example:
std::cin >> file; // User inputs C:\Users\Andrew Finnell\Documents\MyFile.txt
The result is "C:\Users\Andrew", quite unexpected considering that the data is not consumed until the newline and, the next std::string read will automatically be consumed and filled with "Finnell\Documnts\MyFile.txt"
std::getline(std::cin, file);
This will consume all text until the newline.
回答2:
You forgot to include <string>
, which is where that function is defined. Remember that each type defines its own operator>>
as a static function for manipulation via a stream. an input stream could not possibly be written to account for all types that may be created in the future, so it is extended this way.
来源:https://stackoverflow.com/questions/10183008/cin-has-no-operand