Using typedefs from other classes

This is a discussion on Using typedefs from other classes within the C++ Programming forums, part of the General Programming Boards category; Hi, I have created a public typedef in one class, but I want to use it in another class. Let ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    Using typedefs from other classes

    Hi,

    I have created a public typedef in one class, but I want to use it in another class. Let me clarify using an example:

    Code:
    class WhereTypeIsDeclared
    {
    public:
          typedef enum {RED, GREEN, BLUE, WHITE} Colors;
    };
    And now I'm using it in another class:

    Code:
    #include WhereTypeIsDeclared // I've used includes AND foward declaration
    class WhereTypeIsDeclared;
    
    class WhereTypeIsUsed
    {
    public:
          void readColor(Colors* c);
    };
    How do I make this thing work? Thanks in advance.

    Renan M Z Mendes

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What about
    Code:
        void readColor(WhereTypeIsDeclared::Colors* c);

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Colors *c ?

    Any idea why you would need a pointer to an enumerated constant? It's a read-only value, and there isn't any overhead in passing Colors by value.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by citizen View Post
    Colors *c ?

    Any idea why you would need a pointer to an enumerated constant? It's a read-only value, and there isn't any overhead in passing Colors by value.
    Since the function name is "readColor", I'm guessing the value of c is going to change inside the function. Why we don't use a reference, I don't know, I'm just copying. Edit: or just return a Colors, although we would still have to use the class name namespace.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,203
    The forward declaration, on its own, is neither necessary nor sufficient to be able to use the typedef. You need definition of the class, and then you need to use scoping constructs to access the typedef.

    Code:
    class WhereTypeIsDeclared
    {
    public:
          enum {RED, GREEN, BLUE, WHITE} Colors;
    };
    
    
    class WhereTypeIsUsed
    {
    public:
          void readColor(WhereTypeIsDeclared::Colors* c);
    };

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Thanks, everyone. I forgot about specifying where the type came from. So,

    Code:
    WhereTheTypeIsDefined::Colors* c
    really did the trick.

    Thanks once again,


    Renan M Z Mendes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Using typedefs within classes.
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2006, 10:17 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21