0

I've got this "multiple Definitions" Error and don't know who to fix it.

header.h

    #ifndef HEADER_H
     #definde HEADER_H

     enum Gamestate{
         MENU,
         PLAY,
         PAUSE,
         GAMEOVER
        };
     Gamestate GAMESTATE = MENU;

#endif

main.cpp

#include "header.h"

switch(GAMESTATE){...}

If I put the Gamestate GAMESTATE = MENU; in the header.cpp main.cpp doesn't know the variable. If I compile it this way I get the multiple Def . Error.

0

2 Answers 2

2

You should declare the global variable extern in the header:

 #ifndef HEADER_H
 #definde HEADER_H

 enum Gamestate{
     MENU,
     PLAY,
     PAUSE,
     GAMEOVER
    };

 extern Gamestate GAMESTATE;

 #endif

and provide a definition in any of your .cpp files:

 Gamestate GAMESTATE = MENU;
Sign up to request clarification or add additional context in comments.

Comments

1

It means that header "header.h" is included in more than one compilation unit.

In this case variable GAMESTATE is defined in each module that includes the header.

You should declare the variable without its definition in the header the following way

extern Gamestate GAMESTATE;

and then for example in main.cpp define it like

Gamestate GAMESTATE = MENU;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.