【问题标题】:cin.get() causing "no instance of overloaded function" errorcin.get() 导致“没有重载函数的实例”错误
【发布时间】:2019-09-15 19:48:08
【问题描述】:

我正在为一个班级工作的 C++ 项目遇到一些问题。我不断收到一条错误消息,指出“没有重载函数的实例”。我做了一些谷歌搜索,似乎每个人都说这个错误是由将字符串传递给 cin.get() 函数引起的,但我将此函数与 char 一起使用,而不是字符串。 Visual Studio 说错误在:“cin.get(nameFull);”但我已将 nameFull 定义为字符,而不是字符串。任何帮助将不胜感激,感谢您的宝贵时间。

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


    int main()
    {
        const int MONTHS = 12;
        const int RETRO_MONTHS = 6;

        char nameFull[30];      // INPUT - Employee's full name
        float salaryCurrent;    // INPUT - Current annual salary
        float percentIncrease;  // INPUT - Percent increase due
        float salaryNew;        // OUTPUT - New salary after increase
        float salaryMonthly;    // OUTPUT - New monthly salary
        float retroactivePay;   // OUTPUT - Retroactive pay due employee
        int count;              // CALC - Counter for loop

        for (int count = 0; count < 3; count++) {
            cout << "What is your name?" << endl;
            cin.get(nameFull);
            cout << "What is your current salary?" << endl;
            cin >> salaryCurrent;
            cout << "What is your pay increase?" << endl;
            cin >> percentIncrease;

            salaryNew = salaryCurrent + (salaryCurrent * percentIncrease);
            salaryMonthly = salaryNew / MONTHS;
            retroactivePay = (salaryNew - salaryCurrent) * RETRO_MONTHS;

            cout << nameFull << "'s SALARY INFORMATION" << endl;

            cout << "New Salary"
                << setw(20) << "Monthly Salary"
                << setw(20) << "Retroactive Pay" << endl;

            cout << setprecision(2) << fixed << setw(10) << salaryNew
                << setw(20) << salaryMonthly
                << setw(20) << retroactivePay << endl;

            cout << "<Press enter to continue>" << endl << endl;
            cin.get();
        }
        return 0; 
}

【问题讨论】:

  • 您的意思是getline (cin, nameFull);? (虽然最好让nameFull 输入std::string)

标签: c++ syntax-error


【解决方案1】:

nameFull 是一个由char(更具体地说是char[30])组成的数组,它衰减为指向字符的指针(char*)。 std::istream::get 没有重载,它接受一个指向字符的指针,但有一个 接受一个指针 + 你想读入的缓冲区的大小。

所以你需要做的就是传递一个额外的参数:

cin.get(nameFull, 30);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多