Reputation: 41
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
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