我已经包含并正在使用标准命名空间,当我将文件名硬编码到 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/