0

I am very new to programming, and am using C++.

in my header file : Globals.h, I have declared both my enum, "Colour" and a function meant to test it.

In my source file, Globals.cpp, I have defined both the enum and the test function.

In main I call the function but am given this message:

Error C2027: Use of undefined type 'Colour'

and

Error C2065: 'COLOUR_BLACK': Undeclared identifier.

My code is below:

Globals.h

#ifndef GlOBALS_H
#define GLOBALS_H

enum class Colour;
void print_Colour(Colour whatColour);

#endif 

and:

Globals.cpp

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


enum class Colour
{
    COLOUR_BLACK,
    COLOUR_BROWN,
    COLOUR_BLUE,
    COLOUR_GREEN,
    COLOUR_RED,
    COLOUR_YELLOW
};

void print_Colour(Colour whatColour)
{
    switch (whatColour)
    {
    case Colour::COLOUR_BLACK:
        std::cout << "Black" << "\n";
        break;
    case Colour::COLOUR_BROWN:
        std::cout << "Brown" << "\n";
        break;
    case Colour::COLOUR_BLUE:
        std::cout << "Blue" << "\n";
        break;
    case Colour::COLOUR_GREEN:
        std::cout << "Green" << "\n";
        break;
    case Colour::COLOUR_RED:
        std::cout << "Red" << "\n";
        break;
    case Colour::COLOUR_YELLOW:
        std::cout << "Yellow" << "\n";
        break;
    default:
        std::cout << "Who knows!" << "\n";
    }
}

and:

Source.cpp

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

using namespace std;

int main()
{
    Colour paint = Colour::COLOUR_BLACK; 
    print_Colour(paint);
    std::cin.get();
    std::cin.get();
    return 0;
};

Any help would be greatly appreciated!

Peter Turner
  • 335
  • 1
  • 4
  • 17

2 Answers2

4

Source.cpp is only seeing the forward declaration. You should move the actual enum into the header file.

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
1

enum class Colour must be defined in the header file Globals.h which you are including in Source.cpp. Otherwise, the definition of this enum is invisible within Source.cpp.

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • Thanks! A question, you say it is invisible, but the function: print_Colour is not. Is this just how enums are? they cannot be defined in a source file unless they are being used in that same source file? – Peter Turner Jul 01 '15 at 09:41
  • Why is the function definition not invisible to source.cpp? – Peter Turner Jul 01 '15 at 09:41
  • @PeterTurner while you [can forward declare functions](http://stackoverflow.com/questions/4757565/c-forward-declaration), you [cannot forward declare enums](http://stackoverflow.com/questions/71416/forward-declaring-an-enum-in-c). – m.s. Jul 01 '15 at 10:41