c++ - 为什么这个基本的 cin 会阻止我的程序编译?

标签 c++

我已经包含并正在使用标准命名空间,当我将文件名硬编码到 in 中时,程序运行得很好,但是当我输入那个 cin 时,VS 会出现奇怪的错误。 为清楚起见,我专门谈论 cin >> sodokuFile 行。

cout << "Assignment 2\n\n";
ifstream ins;
cout << "Please enter the Sokoku file\n";
string sodokuFile;
cin >> sodokuFile;
ins.open(sodokuFile.c_str());

if(ins.is_open())
{
    int num;
    //counting numbers displayed horizontally
    int counth = 0;
    //counting numbers displayed vertically
    int countv = 0;
    while (ins >> num)
    {
        cout << num << "   ";
        counth++;
        //placing vertical lines
        if(counth %3 == 0)
        {
            cout << "| ";
        }
        //making line breaks for new rows
        if(counth == 9)
        {
            cout << "\n\n";
            counth = 0;
            countv++;
            //horizontal lines
            if(countv %3 == 0)
            {

                cout << "_________________________________________\n";
            }
        }
    }   
}
else
{
    cout << "File does not exist\n";
    return 0;
}

return 0 ;

这是编译器错误中唯一看起来有用的东西 error C2679: binary '>>' : 找不到接受类型为 'std::string' 的右手操作数的运算符(或没有可接受的转换)

最佳答案

你需要放

#include <string>

在文件的顶部,因为 string header 声明了 operator>>(istream&, string&)

关于c++ - 为什么这个基本的 cin 会阻止我的程序编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9251678/

相关文章:

c++ - QT Creator 和 OpenCV455 : 'arm_neon.h' file not found

c++ - 使用 std::count 计算字符串 vector 中特定字符的数量 (C++)

c++ - 如何最好地管理游戏引擎中的组件?

c++ - 如何为派生类通用地实现复制构造函数?

c++ - Cpp 返回好奇心

c++ - SDL 和绑定(bind)缓冲区

c++ - 从C++动态2D阵列到OpenCV Mat

c++ - 将结构的值设置为五个可能值之一

c++ - 具有多个参数的部分类模板特化

c++ - 构造函数和赋值运算符