2014-09-19 137 views
-5

我有一个方法,它接受一个浮点指针和一个int指针,然后执行pow(the float, the int)并返回值。我得到了一个很大的错误,我似乎无法理解它告诉我的错误。C++浮点指针int指针

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

float method3(float *f, int *i); //initialize pointer method 

float flt; //init variable 
int nt; //init variable 
int main() { 
    method3(*flt, *nt); //run method 3, which will do the same math, but with pointers instead of value or reference 
    cout << flt; //print it out 
    return 0; 
} 

float method3(float *f, int *i) { //method 3, get float and int by pointers 
    return pow(f, i); //f to power of i back to original flt variable 
} 

请让我知道我在做什么错了?

+1

'* flt'和'* nt'没有任何意义。 “*”的参数必须是一个指针,而不是“float”或“int”。 – Barmar 2014-09-19 18:41:27

+1

'void'返回类型并返回'return pow(f,i);'只是错误的!此外,我看不到任何理由为什么这些参数作为指针传递,而不是简单地按值。 – 2014-09-19 18:43:09

+0

'return pow(* f,* i);'然后,如果你坚持这个奇怪的函数签名! – 2014-09-19 18:47:47

回答

3

您正在取消引用不正确的指针。

你应该叫

pow(*f,*i); 

method3(&flt,&nt); 
+1

另外method3应该返回一些值而不是void – 2014-09-19 18:42:20

+0

好的调用。使用'float method3(float * f,int * i);' – Ian 2014-09-19 18:46:50

0

method3回报void,所以你不能写return pow(f, i);里面。

+0

我知道这一点,那不是问题。但伊恩已经修好了。 – 2014-09-19 18:45:47