错误:无法在赋值时将“double”转换为“double(double,double,double)”

时间:2014-02-27 18:34:20

标签: c++ string double cmath

我知道我的工作很草率,这是我在本课程中的第四次任务。任何帮助将不胜感激,谢谢。

double  getPrincipal(0);
double  getRate(0);
double  getYears(0);
double  computeAmount(double getPrincipal, double getRate, double getYears);
double  displayAmount(double principal, double rate, double years, double amount);

cout << "what is the principal ammount?" << endl;
cin >> getPrincipal;

cout << "What is the percentage rate?" << endl;
cin >> getRate;

cout << "Over how many years will the money stay in the bank?" << endl;
cin >> getYears;  

computeAmount = pow((1 + getRate / 100),getYears); // This is where i got the error

5 个答案:

答案 0 :(得分:3)

通过尝试为函数分配值,您正在使用 functions 搞乱 variables

double  computeAmount(double getPrincipal, double getRate, double getYears);

在此行中,您声明computeAmount()是一个以3 double为参数并返回double的函数。

但是,在这一行中,

computeAmount = pow((1 + getRate / 100),getYears);

您正尝试将其用作变量。

取决于您的目的是什么,您可能想要更改这两行中的一行。例如,您可以删除第一行,并将第二行更改为:

double computeAmount = pow((1 + getRate / 100),getYears);

答案 1 :(得分:1)

正如编译器试图告诉您的那样,您无法将变量赋值给函数

如果你想要它是一个功能,定义它&amp;叫它。

如果您希望它是变量,请将其声明为变量。

答案 2 :(得分:1)

computeAmount是您定义的函数的名称,该函数返回double并且需要3 double个参数。 pow会返回double

将该行更改为

double computedAmount = pow((1 + getRate) / 100, getYears);
       ^^^^^^^^^^^^^^ -- notice this is no longer the function name, but a new variable

答案 3 :(得分:1)

您将名称computeAmount声明为函数名称

double  computeAmount(double getPrincipal, double getRate, double getYears);

所以这句话

computeAmount = pow((1 + getRate / 100),getYears);

毫无意义。由于computeAmount是一个函数名,因此在上面的espression中它被转换为指向函数的指针,并且你试图将函数pow返回的一些double值赋给该指针。

答案 4 :(得分:1)

computeAmount声明为函数,但在'='运算符的左侧使用。 解决方案:将computeAmount重新声明为double:

double computeAmount;