0

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?

  • 5
    You would want to put this enum definition in [_a header file_](https://stackoverflow.com/questions/25757350/what-should-go-in-my-header-file-in-c/25757789). – Drew Dormann Nov 03 '21 at 01:08
  • 1
    (I'd flesh out more details in an answer, but I suspect this question is more suited as a duplicate of my link. But maybe I'm wrong.) – Drew Dormann Nov 03 '21 at 01:16

1 Answers1

3

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.