c - 'invalid operands to binary == (have ' quantity' 和 'int' )' 是什么意思?

标签 c int operand

我正在学习 C,这是“Head First C”一书中的练习,我的代码看起来与示例相同,但我遇到了上述错误。

#include <stdio.h>

typedef enum {
    COUNT,POUNDS,PINTS
}unit_of_measure;

typedef union {
    short count;
    float weight;
    float volume;
}quantity;

typedef struct{
    const char *name;
    const char *country;
    quantity amount;
    unit_of_measure units;
}fruit_order;

void display(fruit_order order)
{
    printf("The order contains ");

    if(order.amount==PINTS) //ERROR HERE
        printf("%2.2f pints of %s\n",order.amount.weight, order.name);

    else if(order.amount==POUNDS){  //ERROR HERE
            printf("%2.2f lbss of %s\n",order.amount.weight, order.name);

    else
            printf("%i %s\n",order.amount.weight, order.name);

}

int main()
{
    fruit_order apples = {"apples","Canada", .amount.count=100, COUNT};

    fruit_order strawberries = {"strawberries","England", .amount.count=100, PINTS};
    fruit_order oj = {"juice","USA", .amount.count=100, PINTS};

    display(apples);
    display(strawberries);
    display(oj);
    return 0;
}

这个错误是什么意思?

最佳答案

你的意思是:

if (order.units == PINTS)

否则,您正在尝试将枚举值与 union 类型进行比较。

关于c - 'invalid operands to binary == (have ' quantity' 和 'int' )' 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18476926/

相关文章:

c - 当进程仍在运行时查找进程及其子进程的 cpu 时间

c++ - 输入结构体中数组的大小

javascript - 添加包含数字的javascript变量时如何防止链

c# - 错误的 IF 语句

c++ - 没有匹配到 'operator<' 错误。 C++

c - 用字符串递归遍历树

c - C 中的嵌套 Lua 元表

python - int(x[,base]) 是如何工作的?

java - 从字符串解析 int 会产生错误的结果

c - C中操作数的顺序