“错误:没有cin的操作符>>”我无法弄清楚我在这里做错了什么

时间:2010-09-21 16:25:36

标签: c++ input compiler-errors

我是一名学生,上周刚刚开始学习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;

}

4 个答案:

答案 0 :(得分:5)

这是一个实际编译的版本。你可以自己弄清楚你错过了什么; - )

#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout << "Enter how many employees you have:";
    int numEmployees = 0;
    cin >> numEmployees;

    for(int i = 0; i < numEmployees; ++i)
    {
        cout << "What is Employee number: " << i << "'s name:";
        string employeeName;
        cin >> employeeName;
    }
}

答案 1 :(得分:2)

总侥幸。

我只是把

 #include<string>

在顶部。

我不知道控制台无法处理字符串

答案 2 :(得分:0)

im getting a compile error saying ""Error no operator matches these commands" even though i just used them 5 lines ago. 

这听起来像命名空间问题。

欢迎来到精彩的编程世界。 ;)

答案 3 :(得分:0)

  

我收到编译错误说Error no operator matches these commands,即使我刚刚使用它们5行之前。

如果这是指您发布的剪辑,那么您错了。与所有其他函数一样,运算符可以在C ++中重载。这意味着可以使用相同名称的几个函数,只要它们采用不同的参数(或者是const或不是成员函数)。

变量名numEmployees在我看来好像是指 ,而employeeName可能是指 字符串 即可。因此,这将调用 operator>>()的两个不同重载 来输入这些变量。

由于我在这里省略的原因,在标题operator>>()中定义了<string>重载到字符串中,而内置类型(int等)的重载是在<istream>中定义,您通常可以通过<iostream>获得。

所以,鉴于你给我们的信息很少,这是一个很长的镜头,但 我想你错过了#include <string>

相关问题