In C++, how do I globally use an enum in a multi-file project?
-
I have an enum declared globally in main.cpp like this... enum STATE {TITLE, LEVEL} state; The player class in player.cpp uses it...When I compile, I obviously get this error because player.cpp doesn't know that STATE exists since it's in main.cpp... player.cpp:68: error: ‘state’ was not declared in this scope player.cpp:68: error: ‘TITLE’ was not declared in this scope player.cpp:71: error: ‘LEVEL’ was not declared in this scope player.cpp:75: error: ‘LEVEL’ was not declared in this scope But when I try to declare it using extern in my global.h file, I get this... global.h:17: error: use of enum ‘STATE’ without previous declaration global.h:17: error: a storage class can only be specified for objects and functions In file included from player.cpp:1: I've tried extern enum STATE and extern STATE state...Nothing seems to work...What's the correct way to do this?
-
Answer:
EDIT: You need to remove the enum list declaration from every file except in the header file, otherwise you will get that error The header file contains the declaration, the .cpp files contain the implementation Place the enumerated list inside the header file (the .h file), then it will automatically be included in any of the .cpp files which references to the header file
Adam at Yahoo! Answers Visit the source
Related Q & A:
- How do I apply the overlay to draw a line in maps Android?Best solution by Stack Overflow
- How can I echo characters before and after a string?Best solution by stackoverflow.com
- How may I delete old attachments from my attachment /photo file?Best solution by Yahoo! Answers
- How do I add/use my American Express gift card on Paypal?Best solution by Yahoo! Answers
- Is there a way I can use my camcorder as a webcam?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.