Cin没有操作数>>

Der*_*erp 3 c++ iostream cin

我不明白为什么这不起作用.出于某种原因,我收到了错误:

error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

如果有帮助,我在Visual Studio2010 C++ Express中这样做.不知道为什么它给我这个错误我已经做了其他程序使用cin...

我的代码:

#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;
    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*ell 6

包括 <string>

最重要的是,我建议您使用getline,因为>>将停止输入中的第一个单词.

例:

std::cin >> file; // User inputs C:\Users\Andrew Finnell\Documents\MyFile.txt
Run Code Online (Sandbox Code Playgroud)

结果是"C:\ Users\Andrew",非常意外,因为在换行之前数据没有被消耗,并且下一个std :: string读取将被自动消耗并填充"Finnell\Documnts\MyFile.txt"

std::getline(std::cin, file); 
Run Code Online (Sandbox Code Playgroud)

这将消耗所有文本,直到换行符.