Conner
Conner

Reputation: 677

Global enum in C++

Ok say I want to define a terrain enum as

enum terrain {MOUNTAIN, GRASS};

or something.

How would I make this enum something that is defined in all the classes in my project?

Upvotes: 5

Views: 9110

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 992857

Put your enum declaration in a header file:

terrain.h

#ifndef TERRAIN_H
#define TERRAIN_H

enum terrain {MOUNTAIN, GRASS};

#endif

(The #ifndef/#define pair is an include guard, you can read about those elsewhere.)

source.cpp

#include <stdio.h>
#include "terrain.h"

// your code here

Include the terrain.h file in every source file where you need it. You can also include a header from another header file if you need to.

Upvotes: 11

Related Questions