Search code examples
cstructdefinitionincomplete-type

Calling '' with incomplete return type ''


Despites all my verifications, I still can't figure what is wrong with the 10 first lines of my program...

typedef struct Case_PN Case_PN;

struct Case_PN {
    unsigned entier;
    unsigned flottant;
    union {
        int i;
        double f;
    } u;
};

Case_PN case_pn_init (unsigned type, int val1 , double val2){
    Case_PN c = {0};
    if (type==0){
        c.entier =1; c.flottant = 0; c.u.i =val1;
    }
    else {
        c.entier =0; c.flottant =1; c.u.f = val2;
    }
    return c;
}

To explain a little bit, Case_PN is just a type union, as an int when entier ==1, and a float in the other cases. case_pn_init initialize such a structure. But the issue is during the compilation :

int main(int argc, const char * argv[]) {
    case_pn_init(0,1,(8.0));
  
    return 0;
}

With a wonderful error message :

Calling 'case_pn_init' with incomplete return type 'Case_PN' (aka 'struct Case_PN')

How can I repair it ? Thank you in advance :)


Solution

  • In the function declaration

    Case_PN case_pn_init (unsigned type, int val1 , double val2){
    

    there is used undefined type Case_PN.

    It seems you mean

    struct Case_PN case_pn_init (unsigned type, int val1 , double val2){
        struct Case_PN c = {0};
        //...
    

    Even if you used a typedef like for example

    typedef struct Case_PN Case_PN;
    
    Case_PN case_pn_init (unsigned type, int val1 , double val2){
    

    then in any case the structure definition shall be before the function definition.

    It seems the translation unit with main where the function is called does not see the structure definition. It sees only the typedef declaration.

    Place the structure definition in the header and include the header everywhere where the function is defined and called.