不兼容的类型时,函数返回一个工会 [英] incompatible types when a function returns a union

查看:108
本文介绍了不兼容的类型时,函数返回一个工会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是code片:

ip.h

typedef union _ip_t{
    struct _dot_ip {
        unsigned char f4;
        unsigned char f3;
        unsigned char f2;
        unsigned char f1;   //the first field
    }dot_ip;
    unsigned int int_ip;
}ip_t;

ip.c

ip_t
get_mask(int sub_len)
{
    assert(sub_len > 0 || sub_len < 32);
    ip_t ret;
    ret.int_ip = ~((1 << (32 - sub_len)) - 1);
    return ret;
}

的main.c

main.c

ip_t mask;
mask = get_mask(24);

那么错误:

错误:不兼容的类型分配从类型键入'ip_t'当'诠释'

error: incompatible types when assigning to type ‘ip_t’ from type ‘int’

面膜= get_mask(24);

mask = get_mask(24);

我想不通的地方是错误的,任何帮助将是pciated AP $ P $

I can't figure out where is wrong, any help will be appreciated

PS:GCC verison:GCC(Ubuntu的4.8.2-19ubuntu1)4.8.2

PS: gcc verison: gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2

推荐答案

您的功能没有声明是在的main.c 可见。在的main.c 的功能是完全未知的编译器。你的编译器认为它返回 INT 。其余如下。

No declaration of your your function is visible in main.c. In main.c the function is completely unknown to the compiler. Your compiler assumed that it returns int. The rest follows.

,编译器的这种行为是C89 / 90特异性的。它已被取缔的C99语言规范。现代的C编译器不应该让你叫未申报的功能。

Such behavior of the compiler is C89/90-specific. It has been outlawed in C99 language specification. Modern C compilers are not supposed to let you call undeclared functions.

原型你的 get_mask 函数添加到 ip.h

ip_t get_mask(int sub_len);

要告诉 get_mask 实际上返回编译器 ip_t

to tell the compiler that get_mask actually returns ip_t.

由于您使用的是 GCC ,我怀疑是编译器实际增发的诊断消息,告知您关于 get_mask 是未申报。你刚才忽略该消息?

Since your are using gcc, I suspect that the compiler actually issued an additional diagnostic message informing you about get_mask being undeclared. Did you just ignore that message?

这篇关于不兼容的类型时,函数返回一个工会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆