Siphenx
Siphenx

Reputation: 21

C++ enum does not name a type

I'm working on a problem where part of the code looks like the one below:

class x
{
  type t;
public:
  enum type { A, B, C, D};
};

g++ says type does not name a type. I tried changing type t to x::type t or compiling with C++11 to no avail.

It's a simple problem so there's no linking, aka, I didn't include any header file.

Please enlighten me.

Upvotes: 1

Views: 4416

Answers (2)

phantom
phantom

Reputation: 3342

You have to declare type before you create a variable of type type. If you move type 't' after the declaration of type in your code it will fix the error. Change this

class x
{
   type t;
public:
   enum type { A, B, C, D};
};

to this

class x
{
public:
    enum type { A, B, C, D};
private:
    type t;
};

and it will work properly.

Upvotes: 3

Axalo
Axalo

Reputation: 2953

The compiler doesn't know type is an enum because it was used before it was declared.

Try this:

class x
{
public:
  enum type { A, B, C, D};
private:
  type t;
};

Upvotes: 1

Related Questions