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

Why does this basic cin prevent my program from compiling?

本文关键字:我的 程序 编译 cin 为什么      更新时间:2023-10-16

我已经包含并正在使用标准名称空间,当我将文件名硬编码到中时,程序运行得很好,但当我放入cin-VS时,会出现奇怪的错误。为了清楚起见,我特别提到cin>>sodokuFile行。

cout << "Assignment 2nn";
ifstream ins;
cout << "Please enter the Sokoku filen";
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 << "nn";
            counth = 0;
            countv++;
            //horizontal lines
            if(countv %3 == 0)
            {
                cout << "_________________________________________n";
            }
        }
    }   
}
else
{
    cout << "File does not existn";
    return 0;
}
return 0 ;

以下是编译器错误中唯一看起来有用的内容错误C2679:binary">>":找不到接受类型为"std::string"的右侧操作数的运算符(或者没有可接受的转换)

您需要放置

#include <string>

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

相关文章: