在另一个函数中使用函数作为参数时出错

时间:2012-11-04 01:42:13

标签: c++

我一直试图在2天内调试这个C ++错误很多个小时,无法弄清楚或在搜索中找到答案。任何人都可以帮我说明如何解决这个问题吗?

错误:

111:44: error: arithmetic on a pointer to the function type 'double (double, int)'
            return (principal * (pow((effective_rate + 1), years_elapsed)));
                                      ~~~~~~~~~~~~~~ ^

相关代码:

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

using namespace std;

using std::ios; 
using std::cin;
using std::cout;
using std::endl;

double effective_rate(double annual_rate, int num_times_compounded=0);
double balance(double annual_rate, double principal, double &years_elapsed, int num_times_compounded=0);

double annual_rate;
int num_times_compounded;
double principal;
double years_elapsed;

int main() {
 //code to get inputs and do printouts
}

double effective_rate(double annual_rate, int num_times_compounded) 
{
    if (num_times_compounded > 0) {
        return (pow((1 + (annual_rate/num_times_compounded)), num_times_compounded) - 1);
    }   else {
        return (pow(e, annual_rate) - 1);
    }
}

double balance(double annual_rate, double principal, double years_elapsed, int num_times_compounded)
{
    if (num_times_compounded > 0) {
[**this is line 111:**] return (principal * (pow((effective_rate + 1), years_elapsed)));
    } else {
        return (principal * (pow( (effective_rate + 1), num_times_compounded) ) );
    }
}

似乎第二个函数没有看到第一个effective_rate函数,并且更改为通过引用传递似乎也不起作用。我必须遗漏一些简单而明显的东西吗?

1 个答案:

答案 0 :(得分:1)

您需要使用其参数调用函数。如果不使用括号及其参数(如果有),则无法调用函数。

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

double effective_rate(double annual_rate, int num_times_compounded=0);
double balance(double annual_rate, double principal, double years_elapsed, int num_times_compounded=0);

double annual_rate;
int num_times_compounded;
double principal;
double years_elapsed;

int main() {
 //code to get inputs and do printouts
}

double effective_rate(double annual_rate, int num_times_compounded) 
{
    if (num_times_compounded > 0) {
        return (pow((1 + (annual_rate/num_times_compounded)), num_times_compounded) - 1);
    }   else {
        return (pow(e, annual_rate) - 1);
    }
}

double balance(double annual_rate, double principal, double years_elapsed, int num_times_compounded)
{
    if (num_times_compounded > 0) {
        return (principal * (pow((effective_rate(annual_rate, num_times_compounded) + 1), years_elapsed)));
    } else {
        return (principal * (pow((effective_rate(annual_rate, num_times_compounded) + 1), num_times_compounded) ) );
    }
}

这样可行,但您需要先定义e

此外,您应该尽可能避免使用全局变量。在您的情况下,您的名称有冲突。例如,您将annual_ratenum_times_compounded定义为全局变量,使用与函数参数完全相同的名称。在这些情况下不会使用全局变量。

编辑:哦,最后,您还应该避免使用using指令。键入std::并不需要花费太多精力,并且可以使代码更安全。

编辑:要回答OP的最后一个问题,您可以使用三元条件运算符,但牺牲了可读性。唯一的另一个我可以通过使用第三个变量来存储三元条件操作的结果然后使用该变量作为句点来完成它。

double balance(double annual_rate, double principal, double years_elapsed, int num_times_compounded)
{
    return (principal * (pow((effective_rate(annual_rate, num_times_compounded) + 1), num_times_compounded > 0 ? years_elapsed : num_times_compounded)));
}

或者使用变量..可能更清洁。

double balance(double annual_rate, double principal, double years_elapsed, int num_times_compounded)
{
    double period = num_times_compounded > 0 ? years_elapsed : num_times_compounded;
    return (principal * (pow((effective_rate(annual_rate, num_times_compounded) + 1), period )));
}