简体   繁体   中英

enum in struct: dereferencing pointer to incomplete type

I'm not sure if I am just blind but i get this dereferencing pointer to incomplete type in following code: header:

enum foo {stuff1, stuff2, stuff3};

struct bar {
    enum foo e;
    int x;
    int y;
}; 

file in which header is included:

void func(struct bar *b) {
    switch(b->e) {
    ...
    }
}

The error occures in the switch line, also code completion on b only offers me the ints x and y but not the enum. When reading other people's problems with this error I always see them using something at a point where it was not declared yet. But this is not the case here. So why does this code not compile?

It was asked for a complete example of the problematic code. So here it goes: h-file:

enum commandType {ADD_TREE, DEL_TREE, //tree
               ADD_NODE, DEL_NODE, //node
               ADD_SEGM, DEL_SEGM, //segment
               ADD_SKEL, MRG_SKEL, DEL_SKEL,  //skeleton
               ADD_BRCH, JMP_BRCH, //branchpoint
               ADD_COMM, DEL_COMM, //comment
               CHG_NODE, CHG_TREE //change active
};

struct SkelCommand {
    enum commandType type;
    int32_t id;
    int32_t prevActiveId;
};

c-File:

struct stack *undoStack = NULL;

void undo() {
//popStack returns a void*
struct skelCommand *cmd = (struct skelCommand*) popStack(undoStack);

switch(cmd->type) {
    case ADD_TREE:
        break;
    case DEL_TREE:
        break;
    case ADD_NODE:
        break;
    case DEL_NODE:
        break;
    case ADD_SEGM:
        break;
    case DEL_SEGM:
        break;
    case ADD_SKEL:
        break;
    case MRG_SKEL:
        break;
    case DEL_SKEL:
        break;
    case ADD_BRCH:
        break;
    case JMP_BRCH:
        break;
    case ADD_COMM:
        break;
    case DEL_COMM:
    break;
    case CHG_NODE:
        break;
    case CHG_TREE:
        break;
    }
}

in another c-file:

extern struct stack *undoStack;

void initialize() {
    undoStack = newStack(4069);
}

Typo:

struct skelCommand *cmd = (struct skelCommand*) popStack(undoStack);

but the struct is SkelCommand , uppercase S :

struct SkelCommand
{
    enum commandType type;
    int32_t id;
    int32_t prevActiveId;
}; 

Hence the error, as struct skelCommand is an incomplete type.

You don't need the enum keyword in your struct.

Try:

 
 
 
  
  struct bar { foo e; int x; int y; }
 
  

Edit: The above is incorrect and wildplasser is correct. The actual problem seems to be the fact that your func function takes in a struct bar* b . I tested with a C file in VS2010 and removing the struct keyword resolves your problem. Eg:

 
 
 
  
  void func(bar *b) { switch(b->e) { ... } }
 
  

More edits:

Turns out what MS thinks is C and what is actual C is not the same (at least in VS2010). See comments below for the correct answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM