By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,158 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,158 IT Pros & Developers. It's quick & easy.

enum type and classes

P: n/a
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 ?
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Jul 22 '05 #2

P: n/a

"Jorge Rivera" <jo*****@rochester.rr.com> wrote in message
news:UC**************@twister.nyroc.rr.com...
I have several classes and each of them need to have a custom type called enum X


Why do you need this?

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

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.


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

Jul 22 '05 #3

P: n/a
Kaila,

you need to use macros to avoid redefinitions
//X.hpp #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.
Jul 22 '05 #4

P: n/a

"Jorge Rivera" <jo*****@rochester.rr.com> wrote in message
news:44******************@twister.nyroc.rr.com...
Kaila,

you need to use macros to avoid redefinitions
> //X.hpp

#ifndef _XENUMDEF_
#define _XENUMDEF_


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

Thanks
Jul 22 '05 #5

P: n/a
On Mon, 24 May 2004 02:27:12 GMT, Jorge Rivera
<jo*****@rochester.rr.com> wrote in comp.lang.c++:
Kaila,

you need to use macros to avoid redefinitions
> //X.hpp

#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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.