gpt4 book ai didi

c++ - 错误 "cannot convert ' float '到 'float...' ..."

转载 作者:太空宇宙 更新时间:2023-11-04 15:50:12 24 4
gpt4 key购买 nike

这里可能还有其他小的格式错误需要我修复等等,但我需要帮助的是如何处理以下内容:

Lab8pt1.cpp: In function 'float Salary(float, float)':
Lab8pt1.cpp:48: error: assignment of function 'float Salary(float, float)'
Lab8pt1.cpp:48: error: cannot convert 'float' to 'float ()(float, float)' in assignment
Lab8pt1.cpp:50: error: assignment of function 'float Salary(float, float)'
Lab8pt1.cpp:50: error: cannot convert 'double' to 'float ()(float, float)' in assignment
Lab8pt1.cpp:51: error: cannot convert 'float (*)(float, float)' to 'float' in return

我知道它指的是我的 Salary 函数,但我不确定我的 float 有什么问题。这应该只是一个简单的 Lab Assignment,教我们如何使用函数(我们只需要编写函数的代码,其余的交给我们)。

请帮忙!提前致谢!

#include <iostream>
#include <iomanip>
#include <string>

using namespace std ;

void Header(void) ;
float Salary(float Hours, float Pay_Rate);
void Print_it(float Hours,float Pay_Rate,float Sal, float Tax_Rate, string Name);
void Read(float &hour, float &Pay_R,string &name) ;
bool Verify(float Hours, float Pay_Rate);

int main ( void )
{
float Pay_Rate, Hours, Sal, Tax;
const float Tax_Rate= (float)0.09 ;
string name;

Header();
for(int i = 0 ; i < 3 ; i++){
Read(Hours,Pay_Rate,name);
Sal = Salary(Hours,Pay_Rate);
Print_it(Hours,Pay_Rate,Sal, Tax_Rate,name);
}
cout<<"\n\n\n**********\t End of report \t*****\n\n\n\n";
return 0 ;
}

void Header( void )
{
string name;
cout << "Welcome, " << name << ", to the Salary Calculator: a program that will calculate your salary.";
return;
}

float Salary(float Hours, float Pay_Rate)
{
if( Hours <= 40 )
Salary = Hours * Pay_Rate;
else if( Hours > 40)
Salary = Hours * (Pay_Rate * 1.5);
return(Salary);
}

void Print_it(float Hours,float Pay_Rate,float Sal, float Tax_Rate, string Name)
{
cout << fixed << setprecision(2);
cout << "Name: " << left << setw(15) << Name << endl;
cout << "Hours worked: " << left << setw(15) << Hours << endl;
cout << "Pay rate: " << left << setw(15) << Pay_Rate << endl;
cout << "Tax rate: " << left << setw(15) << Tax_Rate << endl;
cout << "Salary: " << left << setw(15) << Sal << endl;
return;
}

void Read(float &hour, float &Pay_R,string &name)
{
cout << "Please enter your name: ";
getline(cin, name);
cout << "Please enter number of hours worked: ";
cin >> hour;
cout << "Please enter your pay rate: ";
cin >> Pay_R;
return;
}

bool Verify(float Hours, float Pay_Rate)
{
if( Hours < 0 || Hours > 60 || Pay_Rate < 0 || Pay_Rate > 500)
return false;
else
return true;
}

最佳答案

Salary = Hours * Pay_Rate;

Salary 是函数名称。您不能为其分配浮点值。您需要声明一个 float 变量并返回该变量。

float sal;

sal = Hours * Pay_Rate;

return sal;

事实上你不需要这个变量。您可以直接在 if-else block 内返回。

if( Hours <= 40 )
return Hours * Pay_Rate;

请注意,方法和变量名应以小写字母开头,类名应以大写字母开头。这是广泛使用的惯例。

关于c++ - 错误 "cannot convert ' float '到 'float...' ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9783514/

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