Cin has no operand >>

 ̄綄美尐妖づ 提交于 2019-12-12 16:08:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!