473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Making Forward Declaration Class's enum Member Visible

hello all,

how can i make, a forward declaration class's enum member, being
visible by another class?

consider the following case,

----------------------------
dog.h
----------------------------
#ifndef DOG_H
#define DOG_H

// class forward declaration.
class cat;

class dog
{
public:
enum dog_enum
{
d0, d1, d2
};

void speak(cat *c);
};

#endif

----------------------------
cat.h
----------------------------
#ifndef CAT_H
#define CAT_H

#include "dog.h"

class cat
{
public:
void speak(dog *d, dog::dog_enum e);
};

#endif

The above cat and dog just work fine. Now, let me create an enum type
for cat too.

----------------------------
dog.h
----------------------------
#ifndef DOG_H
#define DOG_H

// class forward declaration.
class cat;

class dog
{
public:
enum dog_enum
{
d0, d1, d2
};

// OPPS! HOW DO WE FORWARD DECLARE ENUM???
void speak(cat *c, cat::cat_enum e);
};

#endif

----------------------------
cat.h
----------------------------
#ifndef CAT_H
#define CAT_H

#include "dog.h"

class cat
{
public:
enum cat_enum
{
c0, c1, c2
};

void speak(dog *d, dog::dog_enum e);
};

#endif

My question is, how can "dog" see the cat_enum, which is re-inside cat?
I was understand that forward declaration for enum is not allowed in
c++.

Is there any workaround for this?

Thank you very much

Aug 10 '06 #1
3 8841
ya************@gmail.com wrote:
how can i make, a forward declaration class's enum member, being
visible by another class?

consider the following case,

----------------------------
dog.h
----------------------------
#ifndef DOG_H
#define DOG_H

// class forward declaration.
class cat;

class dog
{
public:
enum dog_enum
{
d0, d1, d2
};

void speak(cat *c);
};

#endif

----------------------------
cat.h
----------------------------
#ifndef CAT_H
#define CAT_H

#include "dog.h"

class cat
{
public:
void speak(dog *d, dog::dog_enum e);
};

#endif

The above cat and dog just work fine. Now, let me create an enum type
for cat too.

----------------------------
dog.h
----------------------------
#ifndef DOG_H
#define DOG_H

// class forward declaration.
class cat;

class dog
{
public:
enum dog_enum
{
d0, d1, d2
};

// OPPS! HOW DO WE FORWARD DECLARE ENUM???
void speak(cat *c, cat::cat_enum e);
};

#endif

----------------------------
cat.h
----------------------------
#ifndef CAT_H
#define CAT_H

#include "dog.h"

class cat
{
public:
enum cat_enum
{
c0, c1, c2
};

void speak(dog *d, dog::dog_enum e);
};

#endif

My question is, how can "dog" see the cat_enum, which is re-inside cat?
I was understand that forward declaration for enum is not allowed in
c++.

Is there any workaround for this?
Yes: pull the enum outside the class, putting it in the same namespace
as its associated class. Then in cat.h, you can do:

namespace Canine
{
enum dog_enum;
class dog;
}

class cat
{
// ...
void MewAt( Canine::dog*, Canine::dog_enum );
};

and in dog.h, you can do:

namespace Feline
{
enum cat_enum;
class cat;
}

class dog
{
// ...
void BarkAt( Feline::cat*, Feline::cat_enum );
};

Cheers! --M

Aug 10 '06 #2
mlimber wrote:
Yes: pull the enum outside the class, putting it in the same namespace
as its associated class. Then in cat.h, you can do:

namespace Canine
{
enum dog_enum;
class dog;
}
namespace Feline // Forgot this... sorry
{
enum cat_enum { c1, c2 };
class cat
{
// ...
void MewAt( Canine::dog*, Canine::dog_enum );
};
}
>
and in dog.h, you can do:

namespace Feline
{
enum cat_enum;
class cat;
}
namespace Canine
{
enum dog_enum { d1, d2 };
class dog
{
// ...
void BarkAt( Feline::cat*, Feline::cat_enum );
};
}

Cheers! --M

Aug 10 '06 #3
ya************@gmail.com wrote:
My question is, how can "dog" see the cat_enum, which is re-inside cat?
I was understand that forward declaration for enum is not allowed in
c++.

Is there any workaround for this?
Declare the enums separately from the classes. Use a namespace
if you like.
Aug 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
11
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't...
2
by: John Ratliff | last post by:
I'm having issues with forward declarations and possibly member variables. Can you declare a member variable and pass it parameters. class x { private: y obj(this); } Is that valid? I'm...
3
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward...
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
7
by: Noah Roberts | last post by:
I have something like the following: template<ENUM X, int I = 0> class Generic { .... }; typedef Generic<Val1> v1_type; typedef Generic<Val2> v2_type;
1
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the...
4
by: Steve | last post by:
Hi, I always though to return an instance of a class by value, it had to be defined - i.e. forward declaration isn't good enough? Consider the following code snippet: class RGBA; class...
6
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.