enum type and classes

K

Kaila

I'm using metroworks codewarrior and can't solve a redeclariotin of type
problem

I have several classes and each of them need to have a custom type called
enum X
now what I was thinking is to put the type in a seperate file and simply
attach it as a librabry to my main as follows:

In main.cpp

#include "X.hpp" //file with the enum type
#include "classe1.hpp" //classes...
#include "classe2.hpp"

If I leave it like that my classes do not see the declaration of a type X

If I attach the file into each class then I have a redeclaration problem as
my main sees the declaration twice (+1 once from the main)

I have tried to declare the enum in the private part of each class and have
the private before public but even then my definitions of each method within
the class do nott see the type.

I have tried to play with the linking order of each file without succes with
various combinations but I think there must be a simple way to have an enum
type being visibles in all files without redeclaration problems ?
 
J

Jorge Rivera

Your post is fairly generic. Without code it is hard to understand your
problem...
I have several classes and each of them need to have a custom type called
enum X

Why do you need this?
now what I was thinking is to put the type in a seperate file and simply
attach it as a librabry to my main as follows:

In main.cpp

#include "X.hpp" //file with the enum type
#include "classe1.hpp" //classes...
#include "classe2.hpp"

If I leave it like that my classes do not see the declaration of a type X
Of course not. Each class must individually include X.hpp.
If I attach the file into each class then I have a redeclaration problem as
my main sees the declaration twice (+1 once from the main)

I think you have already solved the problem.
Just have both classe1.hpp and classe2.hpp include X.hpp.
I have tried to declare the enum in the private part of each class and have
the private before public but even then my definitions of each method within
the class do nott see the type.

I have no idea what you are trying to say here...
I have tried to play with the linking order of each file without succes with
various combinations but I think there must be a simple way to have an enum
type being visibles in all files without redeclaration problems ?
This will not help.

JLR
 
K

Kaila

Jorge Rivera said:
Why do you need this?


I want to have an object in each class that is declared of that type

I think you have already solved the problem.
Just have both classe1.hpp and classe2.hpp include X.hpp.

As I said, if I include the X.hpp in both classe1.hpp and classe2.hpp then
the main sees both declarations and complains that there is a type
redeclaration

here is some code that may help you understand

//X.hpp

enum X {X1=0, X2, X3};

//Class1.hpp

some code
#include "X.hpp"
some more code

//Classe1.cpp

Classe1 definitions...



//Classe2.hpp

some code
#include "X.hpp"
some more code

//Classe2.cpp

Classe2 Definitions....




//main.cpp

some code
Classe1.hpp
Classe2.hpp
some more code




In this case my compiler complains that the type X is redeclared
 
J

Jorge Rivera

Kaila,

you need to use macros to avoid redefinitions
#ifndef _XENUMDEF_
#define _XENUMDEF_
enum X {X1=0, X2, X3};
#endif


//Class1.hpp
#ifndef _CLASS1DEF_
#define_CLASS1DEF_

....
#endif


Let me know if this fixes your problem...

Jorge L.
 
K

Kaila

Jorge Rivera said:
Kaila,

you need to use macros to avoid redefinitions

#ifndef _XENUMDEF_
#define _XENUMDEF_

duh I forgot to put it for the X.hpp, it works fine now.

Thanks
 
J

Jack Klein

Kaila,

you need to use macros to avoid redefinitions

#ifndef _XENUMDEF_
#define _XENUMDEF_

Using include guard macros is generally a good idea, but they should
be in the programmer's namespace and not in the namespace reserved for
the implementation. The symbols you have defined above are illegal.

All symbols beginning with an underscore followed by an upper case
letter, or all symbols containing two consecutive underscores anywhere
within them, are reserved for the implementation by the C++ standard
and are not to appear in user code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top