enum type and classes

Discussion in 'C++' started by Kaila, May 24, 2004.

  1. Kaila

    Kaila Guest

    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 ?
     
    Kaila, May 24, 2004
    #1
    1. Advertising

  2. Kaila

    Jorge Rivera Guest

    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
     
    Jorge Rivera, May 24, 2004
    #2
    1. Advertising

  3. Kaila

    Kaila Guest

    "Jorge Rivera" <> wrote in message
    news:UCcsc.354$...
    > > 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
     
    Kaila, May 24, 2004
    #3
  4. Kaila

    Jorge Rivera Guest

    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.
     
    Jorge Rivera, May 24, 2004
    #4
  5. Kaila

    Kaila Guest

    "Jorge Rivera" <> wrote in message
    news:44dsc.78829$...
    > 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
     
    Kaila, May 24, 2004
    #5
  6. Kaila

    Jack Klein Guest

    On Mon, 24 May 2004 02:27:12 GMT, Jorge Rivera
    <> 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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, May 24, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Sosman
    Replies:
    3
    Views:
    800
    Mayeul
    Feb 26, 2010
  2. Brian
    Replies:
    4
    Views:
    2,733
    Brian
    Feb 27, 2010
  3. Wojtek
    Replies:
    1
    Views:
    516
  4. Lew
    Replies:
    0
    Views:
    550
  5. Roedy Green
    Replies:
    0
    Views:
    749
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page