Wrong Soup
Wrong Soup

Reputation: 41

How do I use and enum in multiple files c++

I want to use an enum in multiple different files but I don't know how to. For instance, if in one cpp file I have something like enum Numbers { one = 1, two = 2 }

how can I use this enum in a different file?

Upvotes: 0

Views: 455

Answers (1)

Hexamonious
Hexamonious

Reputation: 41

You can put the enum definition in a header file. For example:

//numbers.h

#pragma once
enum Numbers { one = 1, two = 2 };

And then you can use these enums in any source file, provided you include the header:

//source.cpp

#include <iostream>
#include "numbers.h"

void printNumber(Numbers num)
{
    std::cout << num << std::endl;
}

And this way, you can have access to the enum across any source file.

Upvotes: 3

Related Questions