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

时间:2012-02-12 18:58:19

标签: c++

我已经包含并且正在使用标准命名空间,当我只是硬编码文件名时程序运行得很好,但是当我输入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 ;

这是编译器错误中唯一看起来有用的东西 错误C2679:二进制'&gt;&gt;' :没有找到哪个运算符采用'std :: string'类型的右手操作数(或者没有可接受的转换)

1 个答案:

答案 0 :(得分:6)

你需要把

#include <string>

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