为什么这个基本的cin阻止我的程序编译? [英] Why does this basic cin prevent my program from compiling?

查看:246
本文介绍了为什么这个基本的cin阻止我的程序编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经包括并使用标准命名空间,程序运行正常,当我只是硬编码的文件名进入,但是当我把cin VS给我奇怪的错误。
为了清楚起见,我具体谈论cin >> sodokuFile行。

I've included and am using the standard namespace, and the program runs just fine when I just hard code the file name into in, but when I put in that cin VS gives me weird errors. I'm specifically talking about the cin >> sodokuFile line, for clarity.

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'的右侧操作数(或者没有可接受的转换)

Here is the only thing in the compiler errors that looks useful error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

推荐答案

您需要放置

#include <string>

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

At the top of your file because the string header declares operator>>(istream&, string&).

这篇关于为什么这个基本的cin阻止我的程序编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆