【问题标题】:c++ floating pointer to the power of int pointer时间:2019-01-01 标签:c++floatingpointertopowerintpointer
【发布时间】:2014-11-14 10:21:08
【问题描述】:

我有一个接收浮点指针和整数指针的方法,然后执行pow(the float, the 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
}

请告诉我我做错了什么?

【问题讨论】:

  • *flt*nt 没有任何意义。 * 的参数必须是指针,而不是 floatint
  • void 返回类型并返回 return pow(f, i); 是完全错误的!我也看不出为什么这些参数作为指针传递,而不是简单地通过值传递。
  • return pow(*f, *i); 那么,如果你坚持这个奇怪的函数签名!
  • 如果你的编译器没有向你抛出一堆警告,你需要结束警告级别或使用更好的编译器!我认为将警告级别设置为比舒适级别高一点是一种很好的做法,并且在清除所有警告之前不要运行任何东西。
  • @OP 别大喊大叫,耐心点!

标签: c++ pointers floating-point int cin


【解决方案1】:

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

【讨论】:

  • 我知道,这不是问题。但伊恩已经修好了。
【解决方案2】:

您不正确地取消引用指针。

你应该打电话

pow(*f,*i);

method3(&flt,&nt);

【讨论】:

  • 另外method3应该返回一些值而不是void
  • 好电话。使用float method3(float* f, int* i);
猜你喜欢
  • 2018-01-03
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多