簡體   English   中英

“錯誤:沒有用於cin的運算符>>”我無法弄清楚我在做什么錯

[英]“Error: no operator for cin>>” I can't figure out what I'm doing wrong here

我是一名學生,上周才剛開始學習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;

}

這是實際編譯的版本。 您可以自己弄清楚自己錯過了什么;-)

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

總fl幸。

我只是放

 #include<string>

在頂部。

我不知道控制台無法處理字符串

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

這聽起來像一個名稱空間問題。

歡迎來到精彩的編程世界。 ;)

即時通訊收到一個編譯錯誤,說Error no operator matches these commands即使我在5行之前就使用過它們。

如果這是指您發布的新聞片段,那么您錯了。 與所有其他功能一樣,運算符可以在C ++中重載 這意味着可以有多個使用相同名稱的函數 ,只要它們采用不同的參數(或者是const或不是成員函數)。

變量名numEmployees在我看來就像是引用一個數字 ,而employeeName可能引用一個字符串 因此,這將調用兩個不同operator>>() 重載來輸入這些變量。

由於我在這里省略的原因,在字符串<string>定義了operator>>()重載讀入字符串的定義,而內置類型( int等)的重載定義在<istream> ,您可以通常通過包含<iostream>

因此,鑒於您提供給我們的信息很少,這是一個漫長的嘗試,但我想您可能缺少#include <string>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM