gpt4 book ai didi

C++ 奇怪的错误, "no conversion from ' int' 到 'int (__cdecl *)(float,float)' "

转载 作者:行者123 更新时间:2023-11-30 00:59:01 26 4
gpt4 key购买 nike

首先,这是我关于 stackoverflow 的第一个问题。我正在努力完成家庭作业,但我不知道自己做错了什么。

当我第二次尝试运行一个函数时,出现错误“没有从‘int’到‘int (__cdecl *)(float,float)’的转换”。该函数应该返回 0、-1 或 +1,并在 if/else 语句中使用。

这是我指的代码块...

    #include <iostream>
using namespace std;


////this function returns a -1 if the left pan has a weight more than the right, a 0 if the weights of the two pans are equal, and a +1 if the right pan has a greater weight than the left
int weigh(float leftpan, float rightpan)
{
//compare the pan weights, return value
}


float findOddRock(float r1, float r2, float r3, float r4, float r5, float r6, float r7)
{
//first weigh
float first_leftpan = r1 + r2;
float first_rightpan = r3 + r4;

weigh(first_leftpan, first_rightpan);
if(weigh == 0){
cout << "this program is working so far";
float second_leftpan = r5; //this brings up an error for some reason
float second_rightpan = r6;
weigh(second_leftpan, second_rightpan);


//here's where I get the error, no conversion from 'int' to 'int (__cdecl *)(float,float)'
if(weigh == 0){ //be careful here, changed from second_weigh to weigh
float third_leftpan = r5;
float third_rightpan = r7;
weigh(third_leftpan, third_rightpan);
}

//
int main()
{
//find the light rock
findOddRock(2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0);
}

最佳答案

看起来您想将函数的返回值与 0 进行比较:

 weigh(first_leftpan, first_rightpan);
if(weigh == 0){

尝试做:

if(weigh(first_leftpan, first_rightpan) == 0){

weight 函数也没有返回任何东西..解决这个问题。

如果您尝试使用 == 比较两个 float ,请注意 float 不准确。

关于C++ 奇怪的错误, "no conversion from ' int' 到 'int (__cdecl *)(float,float)' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5086547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com