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 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
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 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 { /* .... */ }
|
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...
|
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...
|
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...
|
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 {...
|
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;
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |