【问题标题】:C++ Strange error, "no conversion from 'int' to 'int (__cdecl *)(float,float)'"C++ 奇怪的错误,“没有从 'int' 转换为 'int (__cdecl *)(float,float)'”
【发布时间】:2011-02-23 02:59:09
【问题描述】:

首先这是我关于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);
 }

【问题讨论】:

    标签: c++


    【解决方案1】:

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

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

    尝试做:

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

    函数weight 也没有返回任何东西..修复它。

    如果您尝试将两个浮点数与== 进行比较,请注意浮点数不准确。

    【讨论】:

    • 为了简洁起见,我没有把整个称重函数放在那里,但我确实将它设置为返回值。编辑:那么关于浮点不准确的整个事情是怎么回事?
    • @Charli 您正在将浮点数与整数进行比较。这意味着在比较中不考虑小数点后的任何内容。您要做的是将浮点变量与浮点数进行比较。
    • 如何更改函数以使其返回某些内容?我想返回字符串,只是说哪块石头是奇怪的,它是什么重量。
    • @Reno 当您比较浮点数和整数时,整数被提升为浮点数。没有错误。
    【解决方案2】:

    上面的人都说对了,不过还是说说错误信息告诉你的吧。

    weigh 是一个函数,它接受两个 floats 并返回一个 int,如您所知。

    所以。调用weigh 得到的值是int。所以你的基本思路是正确的。

    然而,当你写if(weigh == 0) 时,你遗漏了一个重要的部分,()。这就是 C/C++ 中调用函数的语法。 weigh,那么就是函数的名字,和它的“地址”一样。 C/C++中的函数全名包含参数,所以函数全名真的是

    • C 风格函数_cdecl
    • 返回int
    • 接受两个参数(float, float)

    符号weigh 是该函数的地址,由于复杂的原因,我想你还没有看到它,将weigh 的完整类型变成int(_cdecl*)(float,float)

    所以错误消息的意思是“整数比较应该在两个ints 之间进行,但你给我的是一个int 和一个带有两个浮点参数的函数的地址返回 em> int。我太笨了,无法理解。”

    【讨论】:

      【解决方案3】:

      问题出在这里:

      if(weigh == 0)
      

      您正在尝试将函数 weigh 与零进行比较,而不是比较包含返回值的变量。

      【讨论】:

      • 我接受了建议,现在程序运行了,但是 findOddRock 没有返回我包含的任何字符串。阻止它返回任何字符串的函数有什么问题?
      【解决方案4】:

      比较

      (weigh == 0)
      

      将函数指针与整数进行比较。错误出现在第二个实例而不是第一个实例上的原因是编译器的工件。我相当肯定,如果您注释掉给您错误的行,您将在第一个时收到类似的错误。 (尽管一些编译器可能会隐式地将 0 转换为指针并允许比较......在这种情况下,您可能会因为缺少右括号而看到错误?)

      无论如何,您似乎想要比较调用的结果来称重,在这种情况下,您应该捕获返回值并在比较中使用它。

      int ret = weigh(first_leftpan, first_rightpan);
      if(ret == 0){
      ...
      

      【讨论】:

        【解决方案5】:

        这是怎么回事:

        if(weigh == 0)
        

        为什么要将函数与 0 进行比较?

        我认为你真的打算这样做

        if(0 == weigh( blah, blah ))
        

        接下来解决你的逻辑问题

        您正试图找到重量最轻的石头。 你应该使用的是排序。既然你是这个 C 的新手,请阅读 this article

        创建一个对浮点数组进行排序的函数。最低元素(或升序数组中的第一个元素)是奇数石。

        【讨论】:

          【解决方案6】:

          if (weigh == 0) 您正在尝试将函数的地址(作为左值的函数名称)与零进行比较

          weigh == 0 永远不会是真的,正如你定义的那样。

          我相信这就是你想要的:

          int result = weigh(first_leftpan, first_rightpan)
          if ( result == 0)
          

          【讨论】:

          • 那不是函数的地址。 int (* func)(float, float) = weigh; 是一个函数的地址。
          猜你喜欢
          • 2017-07-10
          • 1970-01-01
          • 2021-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-24
          • 2020-08-07
          相关资源
          最近更新 更多