C++ 错误:'int' 和 'float*' 类型的无效操作数到二元运算符 '/'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22917043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:16:12  来源:igfitidea点击:

Error: invalid operands of types 'int' and 'float*' to binary operator '/'

c++

提问by Baalzamon

Error: invalid operands of types 'int' and 'float*' to binary'operator/'
int *average = new int((num) / data);

showing up for this line of code. Why so?

出现在这行代码中。为什么这样?

float *data;
int num;
int mode;
double average, median;

cout << "How many students were surveyed?  ";
cin >> num;
data = makeArray(num);

float getAverage(float *data, int num)
{
    int *average = new int((data) / num);
    return *average;
}

采纳答案by UpAndAdam

It means you are comparing two incompatible types together. One of numand datais an int, and the other is a float*. Depending upon the behavior you want you will want to

这意味着您正在比较两种不兼容的类型。其中一个numanddataint,另一个是float*。根据你想要的行为,你会想要

  1. Dereference the pointer, as in *xfor whichever xis the pointer
    2a. You will want to cast the intto a floatfor floating point division, where result is converted back to an int
    2b. You will want to cast the floatto an int, for integer division, which will then be converted back to an int.
  1. 取消对指针的引用,就像指针 2a中*x的任何一个一样。您将要施放到的,其中结果被转换回一个 2B。您需要将 转换为, for ,然后将其转换回。x
    intfloatfloating point divisionint
    floatintinteger divisionint

Update
Since you updated your code I'll point out a bigger problem; you are now leaking memory.

更新
既然你更新了代码,我会指出一个更大的问题;你现在正在泄漏内存。

I would suggest you consider instead returning your integer by value and potentially pass by reference or constant reference and avoid pointers entirely here, but more over I would suggest some symmetry in your input parameters as well as const correctness:

我建议您考虑改为按值返回整数,并可能通过引用或常量引用传递,并在此处完全避免指针,但更重要的是,我建议您的输入参数中存在一些对称性以及常量正确性:

//your code:
float getAverage( float *data, int sum )
{    
    //data is a float* and needs to be de-ref'd and casted to int for float but isnt
    int *average = new int( (data) / num );
    //note that now average is a pointer to a newly constructed int that will never be free'd
    return *average;  //because you return it here, where the value will then be implicily converted to a float and be mostly worthless.
}

// In both suggestions I will use symmetric types, I will not introduce dynamic memory and I will use floating point division for maximal accuracy.

// Suggestion one use Const References
float getAverage( const float &data, const int &num)
{
    float result = data / (float) num;
    return result;
}

// Suggestion two use pointers to constants
float getAverage( const float *data, const int *num )
{
    float result = (*data) / float(*num);
    return result;
}

// Suggestion three pass by value since primitives are cheap
float getAverage( float data, int num)
{
    float result = data / (float) num;
    return result;
}