gpt4 book ai didi

c++ - cin.get() 导致 "no instance of overloaded function"错误

转载 作者:行者123 更新时间:2023-11-30 03:14:17 24 4
gpt4 key购买 nike

我正在为一个类(class)工作的 C++ 项目遇到了一些问题。我不断收到一条错误消息,指出“没有重载函数的实例”。我做了一些谷歌搜索,似乎每个人都说这个错误是由将字符串传递到 cin.get() 函数引起的,但我使用这个函数时使用的是字符,而不是字符串。 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;
}

最佳答案

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

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

cin.get(nameFull, 30);

关于c++ - cin.get() 导致 "no instance of overloaded function"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57947677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com