Wayne Hong
Wayne Hong

Reputation: 119

Enum declaration not found when including containing header

I have an enumerator type that is declared in a header file. I would like to use this in a c file that includes this header file. However when compiling I get an error that the enumerator type is not defined. I've tried copying the enumerator declaration to my source file but I get an error: "nested redefinition of ‘enum command_type’"

Could someone explain how to use the enumerator type in my file please? Thank you!

The enumerator:

//command-internals.h
enum command_type
  {
    AND_COMMAND,         // A && B
    SEQUENCE_COMMAND,    // A ; B
    OR_COMMAND,          // A || B
    PIPE_COMMAND,        // A | B
    SIMPLE_COMMAND,      // a simple command
    SUBSHELL_COMMAND,    // ( A )
  };

The usage:

//#include "command-internals.h"
command_type scan(char *buffer)
...

The error: error: unknown type name ‘command_type’

Upvotes: 2

Views: 3241

Answers (2)

Alok Save
Alok Save

Reputation: 206546

With your code compiler cannot understand what is the the type of command_type.
One will typically use a typedef:

//Header file

typedef enum {....} command_type;

//C File

command_type scan(char *buffer)

With this you don't have to remember to keep adding an enum everywhere again.

Upvotes: 1

Carl Norum
Carl Norum

Reputation: 224962

Your prototype should read:

enum command_type scan(char *buffer);

Since you didn't put a typedef on the enum declaration.

Upvotes: 2

Related Questions