c++ - Cin 没有操作数 >>

标签 c++ iostream cin

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

error C2678: binary '>>': no operator found which takes a left-hand operand of type 'std::istream' (或没有可接受的转换)

如果有帮助,我将在 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;
    }
}

最佳答案

包括<string>

最重要的是,我建议您改用 getline,因为 >> 将在您输入的第一个单词处停止。

例子:

std::cin >> file; // User inputs C:\Users\Andrew Finnell\Documents\MyFile.txt

结果是“C:\Users\Andrew”,考虑到数据直到换行符才被消耗,这是相当出乎意料的,下一个读取的 std::string 将自动被消耗并填充为“Finnell\Documnts\MyFile.文本文件”

std::getline(std::cin, file); 

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

关于c++ - Cin 没有操作数 >>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10183008/

相关文章:

c++ - 在末尾使用值修饰符 ('-' 、 '%' ) 解析字符串

c++ - 如何在 g++ 中打印 __int128?

c++ - 没有明显拷贝的复制构造函数错误

C++ 将来自 std::cin 的字符串与来自外部 txt 文件的字符串进行比较

c++ - 使用 cin.ignore 和 cin.clear C++ 时的奇怪行为

c++ - 由空格键分隔的数据

c++ - C++ 中的顺序保留 memcpy

c++ - CMAKE指定不同的源目录

c++ - 用于查找文件名索引的 libzip 函数不起作用 C++

c++ - 更新 qt Creator 中的列表