Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > enum type and classes

Reply
Thread Tools

enum type and classes

 
 
Kaila
Guest
Posts: n/a
 
      05-24-2004
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 ?


 
Reply With Quote
 
 
 
 
Jorge Rivera
Guest
Posts: n/a
 
      05-24-2004
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

 
Reply With Quote
 
 
 
 
Kaila
Guest
Posts: n/a
 
      05-24-2004

"Jorge Rivera" <(E-Mail Removed)> wrote in message
news:UCcsc.354$(E-Mail Removed)...
> > 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



 
Reply With Quote
 
Jorge Rivera
Guest
Posts: n/a
 
      05-24-2004
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.
 
Reply With Quote
 
Kaila
Guest
Posts: n/a
 
      05-24-2004

"Jorge Rivera" <(E-Mail Removed)> wrote in message
news:44dsc.78829$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      05-24-2004
On Mon, 24 May 2004 02:27:12 GMT, Jorge Rivera
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between enum class and enum struct Brian C++ 4 02-27-2010 04:03 PM
Re: an enum question - how to convert enum type to a string? Roedy Green Java 0 02-27-2010 04:52 AM
Re: an enum question - how to convert enum type to a string? Wojtek Java 1 02-27-2010 03:44 AM
Re: an enum question - how to convert enum type to a string? Lew Java 0 02-27-2010 03:40 AM
Re: an enum question - how to convert enum type to a string? Eric Sosman Java 3 02-26-2010 03:36 PM



Advertisments