“错误:cin 没有运算符>>”我不知道我在这里做错了什么

发布于 2024-09-24 04:53:46 字数 1137 浏览 3 评论 0原文

我是一名学生,上周刚刚开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦中的蝴蝶 2024-10-01 04:53:46

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

#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;
    }
}

Here is a version that actually compiles. You can figure out what you missed on your own ;-)

#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;
    }
}
傲影 2024-10-01 04:53:46

完全是侥幸。

我只是放在

 #include<string>

最上面。

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

Total fluke.

i just put

 #include<string>

at the top.

I wasn't aware that the console couldn't handle Strings

來不及說愛妳 2024-10-01 04:53:46

我收到一个编译错误,提示错误没有操作符匹配这些命令即使我刚刚在5行前使用过它们。

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

在我看来,变量名称 numEmployees 似乎引用了一个数字,而 employeeName 可能引用了一个字符串。因此,这将调用 operator>>()两个不同的重载来输入这些变量。

由于我在这里省略的原因,读取字符串的 operator>>() 重载是在标头 中定义的,而那些内置的类型(int 等)在 中定义,通常通过包含 来获得。

因此,考虑到您向我们提供的信息很少,这可能性不大,但是我想您缺少一个#include

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

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, while employeeName likely refers to a string. So this would call two different overloads of operator>>() 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>.

贱人配狗天长地久 2024-10-01 04:53:46
im getting a compile error saying ""Error no operator matches these commands" even though i just used them 5 lines ago. 

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

欢迎来到奇妙的编程世界。 ;)

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

This sounds like a namespace issue.

Welcome to the wonderful world of programming. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文