程序因二进制错误而停止
Program stops with a binary error
我被要求为我的推荐课程创建一个汽油泵程序,但我在运行它时遇到了问题,目前这是编译器在尝试编译代码完整构建对话时输出的主要内容
1> m:\visual studio 2010\projects\referration\referration\main.cpp(56):错误C2678:二进制">>":找不到接受类型为"std::istream"的左侧操作数的运算符(或者没有可接受的转换)
#include <iostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <ctime>
#include <cmath>
#include <string>
#include <Windows.h>
using namespace std;
int reciept();
int pump;
int petrol;
int main()
{
bool exit = false;
int code;
string p1w ("Waiting");
string p2w ("Waiting");
string p3w ("Waiting");
string p4w ("Waiting");
string p1r ("Ready");
string p2r ("Ready");
string p3r ("Ready");
string p4r ("Ready");
if (GetAsyncKeyState(VK_ESCAPE))
{
exit = true;
}
cout << "***************************************************" << endl;
cout << "*Liquid Gold v1.0.0 Last revised 18/07/13 *" << endl;
cout << "*The process of processing transactions is simple,*" << endl;
cout << "*activate a pump by entering its code shown below.*" << endl;
cout << "*After pump operation is verified (generally 10 *" << endl;
cout << "*seconds though this may vary) the attendant *" << endl;
cout << "* will be able to enter the amount of petrol to 3 *" << endl;
cout << "*decimal places which will then be converted based*" << endl;
cout << "*on a predetermined price (which can be altered in*" << endl;
cout << "*price.txt) and once this process is complete a *" << endl;
cout << "*receipt will be created (you will need seperate *" << endl;
cout << "*software in order to print this recipt) and the *" << endl;
cout << "*transaction will be terminated. *" << endl;
cout << "*© Simple Software Solutions 2013 *" << endl;
cout << "***************************************************" << endl << endl;
system("Pause");
while (exit == false)
{
cout << " Pump (1) - " << p1w << " Pump (2) - " << p2w << endl << endl << endl;
cout << " Pump (3) - " << p3w << " Pump (4) - " << p4w << endl << endl << endl;
cin >> "Please enter a pump code:" >> code;
if (code == 1)
{
swap (p1w, p1r);
pump = 1;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else if (code == 2)
{
swap (p2w, p2r);
pump = 2;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else if (code == 3)
{
swap (p3w, p3r);
pump = 3;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else if (code == 4)
{
swap (p4w, p4r);
pump = 4;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else
{
cout << "Invalid pump code entered";
}
reciept();
{
ofstream transactions;
transactions.open ("reciept.txt");
transactions << "****************************************************/n";
transactions << " SALE /n";
transactions << "****************************************************/n /n";
}
}
return 0;
}
我环顾四周,我能找到的唯一错误解决方案是包括我已经做过的,我看不到任何其他解决方案。
有谁比我更善于观察,愿意仔细看看,告诉我哪里出了问题?
我也知道我的代码是一个低效的烂摊子,对此我深表歉意。
更改
cin >> "Please enter a pump code:" >> code;
至
cout << "Please enter a pump code: ";
cin >> code;
您需要更改代码中的所有cin >> "string"
。这并不意味着提示用户输入。相反,您实际上是在尝试写入字符串文字。
只是在杨的回答上添加了一些颜色,这不是标题中所建议的"二进制错误"。错误消息指向binary'>>'
。>>
是一个二进制运算符,二进制运算符接受两个操作数,每侧一个。+
和-
在以下方面起二进制运算符的作用:
1 + 2
var1 - var2
一元运算符只接受一个操作数。&
和-
在以下情况下充当一元运算符:
my_pointer = &n;
int var3 = -5;
您收到的错误信息中的重要部分:
binary">>":找不到接受左侧操作数的运算符类型"std::istream"(或者没有可接受的转换)
是最后一位,"或者没有可接受的转换"。当然有一个>>
运算符接受std::istream
的左手操作数,但没有定义>>
运算符接受右手边的字符串文字,因为字符串文字无法赋值。在这种情况下,std::cin >> myvar
从std::cin
中获取内容,并试图将其放入变量myvar
中,但不可能将任何内容放入像"Please enter a pump code:"
这样的字符串文字中,就像试图做到的那样:
"Please enter a pump code:" = 5;
这显然是无稽之谈。
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 错误:二进制'operator*' 'float'和'float[0]'类型的操作数无效
- 错误 C2679 二进制"<<":未找到采用类型为 'T' 的右侧操作数的运算符
- 错误含义;以二进制形式打印数字
- c++ 错误:二进制"运算符+"类型"矩阵*"和"矩阵*"的操作数无效
- -bash:/a.out:无法执行二进制文件:Exec格式错误
- 程序错误地读取了二进制文件的一个字节
- 输入 1024 后十进制到二进制转换的输出错误?
- 错误:二进制">>":未找到采用类型为"const char [1] 的右操作数的运算符
- 错误 C2679:二进制'<<':找不到采用类型 'overloaded-function' 的右侧操作数的运算符(或者没有可接受的转换)
- 我收到一个错误无效的操作数,类型为 const char [42] 和二进制"运算符+"的双倍数
- 错误:C2679 二进制"==":未找到采用类型 'const std::string' 的右侧操作数的运算符(或者没有可接受的转换)
- 仅捕获异常就可以检测所有二进制文件在C 中读取错误是否足够
- 为模板类编写二进制加法运算符. 编译错误
- 没有变量声明为函数,但错误:二进制表达式的操作数无效
- 错误:二进制表达式的操作数无效(映射使用查找函数错误)
- 提升multi_index示例:错误:二进制表达式的操作数无效
- 错误:二进制表达式的操作数无效
- 收到错误"二进制'[':"const typ"未定义此运算符"或"下标需要数组或指针类型