“错误:cin 没有运算符>>”我不知道我在这里做错了什么
我是一名学生,上周刚刚开始学习 C++,所以这个问题可能水平很低,但我无法弄清楚。
我搜索了一下,但找不到任何结果,或者也许我正在寻找错误的东西。
有两个 cin 部分。一个在循环外接收 int,另一个在循环内接收字符串。
我收到一个编译错误,提示“错误没有操作符匹配这些命令”,尽管我刚刚在 5 行前使用过它们。
有帮助吗?
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
// variable declaration
const double payIncrease = 7.6;
string employeeName;
double initialSalary;
double backPay;
double employeeAnnualSalary;
double employeeMonthlySalary;
int numEmployees;
// streams of information
ofstream outStream;
outStream.open("employeeRecords.txt");
// console io's
cout<<"Enter how many employees you have:";
cin>>numEmployees;
for(int i = 0; i <numEmployees;i++)
{
cout<<"What is Employee number: "<<i<<"'s name:";
cin>>employeeName;
cout<<"How much does that employee earn now: ";
cin>>initialSalary;
}
outStream <<"annual salary was: " << numEmployees;
outStream.close();
return 0;
}
I'm a student and just started learning C++ last week so this question is probably very low level but I can't figure it out.
I've searched around a bit but can't find any results, or maybe I'm looking for the wrong thing.
There are two cin parts. One taking in an int outside the loop, the other taking in a string inside the loop.
I'm getting a compile error saying ""Error no operator matches these commands" even though I just used them 5 lines ago.
Help?
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
// variable declaration
const double payIncrease = 7.6;
string employeeName;
double initialSalary;
double backPay;
double employeeAnnualSalary;
double employeeMonthlySalary;
int numEmployees;
// streams of information
ofstream outStream;
outStream.open("employeeRecords.txt");
// console io's
cout<<"Enter how many employees you have:";
cin>>numEmployees;
for(int i = 0; i <numEmployees;i++)
{
cout<<"What is Employee number: "<<i<<"'s name:";
cin>>employeeName;
cout<<"How much does that employee earn now: ";
cin>>initialSalary;
}
outStream <<"annual salary was: " << numEmployees;
outStream.close();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个实际编译的版本。你可以自己找出你错过了什么;-)
Here is a version that actually compiles. You can figure out what you missed on your own ;-)
完全是侥幸。
我只是放在
最上面。
我不知道控制台无法处理字符串
Total fluke.
i just put
at the top.
I wasn't aware that the console couldn't handle Strings
如果这是指您发布的片段,那么您就错了。与所有其他函数一样,运算符可以在 C++ 中重载。这意味着可以有多个函数使用相同的名称,只要它们采用不同的参数(或者是 const 或不是成员函数)。
在我看来,变量名称
numEmployees
似乎引用了一个数字,而employeeName
可能引用了一个字符串。因此,这将调用operator>>()
的两个不同的重载来输入这些变量。由于我在这里省略的原因,读取字符串的
operator>>()
重载是在标头
中定义的,而那些内置的类型(int
等)在
中定义,通常通过包含
来获得。因此,考虑到您向我们提供的信息很少,这可能性不大,但是我想您缺少一个
#include
。If this refers to the snipped you posted, then you're wrong. As all other functions, operators can be overloaded in C++. This means there can be several functions using the same name, provided they take different arguments (or are either
const
or not member functions).The variable name
numEmployees
looks to me as if it would refer to a number, whileemployeeName
likely refers to a string. So this would call two different overloads ofoperator>>()
for inputting these variables.For reasons I'm omitting here, the
operator>>()
overload reading into a string is defined in the header<string>
, while those for built-in types (int
etc.) are in defined in<istream>
, which you usually get by including<iostream>
.So, given what little information you gave us, this is a long shot, but I suppose you're missing an
#include <string>
.这听起来像是命名空间问题。
欢迎来到奇妙的编程世界。 ;)
This sounds like a namespace issue.
Welcome to the wonderful world of programming. ;)