Question

I have three sources: codeproc.h

typedef enum {typeBool, typeVarDeclaration, typeFuncDeclaration } nodeEnum;
typedef struct varDeclarationNodeType{
    char *varName;                  /* Var Name */
    int defaultType;                /* default value type*/
    int defaultValue;               /* default value*/
    int ndim;                       /* number of dimensions */
    int *dim;                       /* dimensions (expandable) */
} varDeclarationNodeType;

typedef struct {
    char *funcName;
    std::vector<char *> *args;       /* arguments (expandable) */
} funcDeclarationNodeType;

typedef struct {
    bool value;
} boolNodeType;

typedef struct nodeTypeTag {
    nodeEnum type;              /* type of node */

    union {
        boolNodeType boolVal;   /* bools */
        varDeclarationNodeType varDeclaration;      /*var declarations*/
        funcDeclarationNodeType funcDeclaration;
    };
} nodeType;

codeproc.cpp

#include "codeproc.h"
#include "codecontext.h"
...

codecontext.h

#include "codeproc.h"
class Function{
public:
    Function();
    ~Function();
    map<string, Value*> locals;     //local variables
    map<string, Value*> args;       //arguments
    int numOfArgs;                  //number of arguments
    nodeType *entryPoint;           //first statement in function
    Value *lastCallResult;          //variable that contain result from last call
};

errors:

codeproc.h error: 'varDeclarationNodeType' has a previous declaration as 'typedef struct varDeclarationNodeType varDeclarationNodeType'

and like that. How to pre-define struct in this situation?

Was it helpful?

Solution

This part

typedef struct varDeclarationNodeType{
    ...
} varDeclarationNodeType;

Should look like this

typedef struct{
    ...
} varDeclarationNodeType;

And you should use header guards or #pragma once in your header files.

OTHER TIPS

I see no preprocessor #pragma once directives or something to make sure you're not redefining types or including a header file multiple times.

Put include guards in your files to prevent double inclusions.

In your specific case, a simple #pragma once at the top of the header files should prevent the multiple inclusions (firmstanding that all the types necessary have been declared, the above code is just a snippet).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top