468,457 Members | 1,563 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Problem with in-line "typedef" and class cross-reference. Need help.

Hi everybody,

I have the following problem :

B class need A::MyEnum type and A class need B::MyEnum type.
In both case, the class type is incomplete so it is obvious that the
::MyEnum can not be found in the class definition (the compiler
complains)

My problem is the MyEnum types have to be defined inside the class
scopes.

=Do you know a way to solve this problem (keeping the types inside
the classes) ?

Thanks in advance.
Here is a simple source illustrating my problem :

---------------------------------------

#include "B.h"

class B;

class A
{
public: A(B::MyEnum);
typedef MyEnum { red, pink, blue };
};

---------------------------------------

#include "A.h"

class A;

class B
{
public: B(A::MyEnum);
typedef MyEnum { one, two, three };
};

Aug 18 '06 #1
7 4330
fo*****@laposte.net wrote:
I have the following problem :

B class need A::MyEnum type and A class need B::MyEnum type.
In both case, the class type is incomplete so it is obvious that the
>>MyEnum can not be found in the class definition (the compiler
complains)

My problem is the MyEnum types have to be defined inside the class
scopes.

=Do you know a way to solve this problem (keeping the types inside
the classes) ?
There is no way. So, as you "have to" but "cannot", you simply "don't
have to" any more. You must find some other solution, like pulling
both of those enums into a separate class or leave them at the namespace
level.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 18 '06 #2
for....@laposte.net wrote:
Hi everybody,

I have the following problem :

B class need A::MyEnum type and A class need B::MyEnum type.
In both case, the class type is incomplete so it is obvious that the
::MyEnum can not be found in the class definition (the compiler
complains)

My problem is the MyEnum types have to be defined inside the class
scopes.
If they "have to be", then you're stuck. You'll have to find another
job.
>
=Do you know a way to solve this problem (keeping the types inside
the classes) ?

Thanks in advance.
Here is a simple source illustrating my problem :

---------------------------------------

#include "B.h"

class B;

class A
{
public: A(B::MyEnum);
typedef MyEnum { red, pink, blue };
};

---------------------------------------

#include "A.h"

class A;

class B
{
public: B(A::MyEnum);
typedef MyEnum { one, two, three };
};
Can't be done. You can forward declare an enum that is at
namespace/file scope, but not one at class scope (except inside the
classes definition, of course, but that doesn't apply here).

Cheers! --M

Aug 18 '06 #3
Thanks for the information.

To be more practical, we decided to use "boost::shared_ptr" smart
pointers instead of classic C++ pointers in a home-made API.
Consequently, all classes have a "#typedef boost::shared_ptr<APIObject>
Ptr" type definition in the class scope.

Unfortunately, when 2 classes are cross-referenced, this can not work.
As you said, I will have to find another way to do it.
mlimber a écrit :
for....@laposte.net wrote:
Hi everybody,

I have the following problem :

B class need A::MyEnum type and A class need B::MyEnum type.
In both case, the class type is incomplete so it is obvious that the
::MyEnum can not be found in the class definition (the compiler
complains)

My problem is the MyEnum types have to be defined inside the class
scopes.

If they "have to be", then you're stuck. You'll have to find another
job.

=Do you know a way to solve this problem (keeping the types inside
the classes) ?

Thanks in advance.
Here is a simple source illustrating my problem :

---------------------------------------

#include "B.h"

class B;

class A
{
public: A(B::MyEnum);
typedef MyEnum { red, pink, blue };
};

---------------------------------------

#include "A.h"

class A;

class B
{
public: B(A::MyEnum);
typedef MyEnum { one, two, three };
};

Can't be done. You can forward declare an enum that is at
namespace/file scope, but not one at class scope (except inside the
classes definition, of course, but that doesn't apply here).

Cheers! --M
Aug 18 '06 #4

fo*****@laposte.net wrote:
Hi everybody,

I have the following problem :

B class need A::MyEnum type and A class need B::MyEnum type.
In both case, the class type is incomplete so it is obvious that the
::MyEnum can not be found in the class definition (the compiler
complains)

My problem is the MyEnum types have to be defined inside the class
scopes.

=Do you know a way to solve this problem (keeping the types inside
the classes) ?

Thanks in advance.
Here is a simple source illustrating my problem :

---------------------------------------

#include "B.h"

class B;

class A
{
public: A(B::MyEnum);
typedef MyEnum { red, pink, blue };
};

---------------------------------------

#include "A.h"

class A;

class B
{
public: B(A::MyEnum);
typedef MyEnum { one, two, three };
};
Could you do something like:

// Abase.h
struct Abase
{
enum MyEnum { red, pink, blue } ;
} ;

// Bbase.h
struct Bbase
{
enum MyEnum { one, two, three } ;
} ;

// A.h
#include "Abase.h"
#include "Bbase.h"

class A : public Abase
{
public:
A(Abase::MyEnum) ;
} ;

// B.h
#include "Abase.h"
#include "Bbase.h"

class B : public Bbase
{
public:
B(Bbase::MyEnum) ;
} ;

--
Alan Johnson

Aug 18 '06 #5

Alan Johnson wrote:
fo*****@laposte.net wrote:
Hi everybody,

I have the following problem :

B class need A::MyEnum type and A class need B::MyEnum type.
In both case, the class type is incomplete so it is obvious that the
::MyEnum can not be found in the class definition (the compiler
complains)

My problem is the MyEnum types have to be defined inside the class
scopes.

=Do you know a way to solve this problem (keeping the types inside
the classes) ?

Thanks in advance.
Here is a simple source illustrating my problem :

---------------------------------------

#include "B.h"

class B;

class A
{
public: A(B::MyEnum);
typedef MyEnum { red, pink, blue };
};

---------------------------------------

#include "A.h"

class A;

class B
{
public: B(A::MyEnum);
typedef MyEnum { one, two, three };
};

Could you do something like:

// Abase.h
struct Abase
{
enum MyEnum { red, pink, blue } ;
} ;

// Bbase.h
struct Bbase
{
enum MyEnum { one, two, three } ;
} ;

// A.h
#include "Abase.h"
#include "Bbase.h"

class A : public Abase
{
public:
A(Abase::MyEnum) ;
} ;

// B.h
#include "Abase.h"
#include "Bbase.h"

class B : public Bbase
{
public:
B(Bbase::MyEnum) ;
} ;
Sorry, I got the parameters to the constructors backwards, but you get
the idea.

--
Alan Johnson

Aug 18 '06 #6
Thanks very much for the idea of the base classes.

In facts, my first sample was enough specific to describe my problem.

I should have wrote it this way :

---------------------------------------

#include "B.h"

class B;

class A
{
public: A(B::Ptr);
typedef boost::shared_ptr<APtr;
};

---------------------------------------

#include "A.h"

class A;

class B
{
public: B(A::Ptr);
typedef boost::shared_ptr<BPtr;
};

.... where A::Ptr point to A class and B::Ptr point to B class (smart
pointers replace the classic C pointers)

This sample works with C pointers but not with Smart Pointers because
of the class cross-reference.
Alan Johnson a écrit :

// Abase.h
struct Abase
{
enum MyEnum { red, pink, blue } ;
} ;

// Bbase.h
struct Bbase
{
enum MyEnum { one, two, three } ;
} ;

// A.h
#include "Abase.h"
#include "Bbase.h"

class A : public Abase
{
public:
A(Abase::MyEnum) ;
} ;

// B.h
#include "Abase.h"
#include "Bbase.h"

class B : public Bbase
{
public:
B(Bbase::MyEnum) ;
} ;
Sorry, I got the parameters to the constructors backwards, but you get
the idea.
Aug 21 '06 #7
Thanks very much for the idea of the base classes.

In facts, my first sample was not enough specific to describe my
problem.
I should have wrote it this way :

---------------------------------------

#include "B.h"

class B;

class A
{
public: A(B::Ptr);
typedef boost::shared_ptr<APtr;
};

---------------------------------------

#include "A.h"

class A;

class B
{
public: B(A::Ptr);
typedef boost::shared_ptr<BPtr;
};
.... where A::Ptr point to A class and B::Ptr point to B class (smart
pointers replace the classic C pointers)

This sample works with C pointers but not with Smart Pointers because
of the class cross-reference.
Alan Johnson a écrit :

// Abase.h
struct Abase
{
enum MyEnum { red, pink, blue } ;
} ;

// Bbase.h
struct Bbase
{
enum MyEnum { one, two, three } ;
} ;

// A.h
#include "Abase.h"
#include "Bbase.h"

class A : public Abase
{
public:
A(Abase::MyEnum) ;
} ;

// B.h
#include "Abase.h"
#include "Bbase.h"

class B : public Bbase
{
public:
B(Bbase::MyEnum) ;
} ;
Sorry, I got the parameters to the constructors backwards, but you get
the idea.
Aug 21 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by A. Saksena | last post: by
12 posts views Thread by Generic Usenet Account | last post: by
reply views Thread by Olaf Petzold | last post: by
5 posts views Thread by Joe Pizzi | last post: by
11 posts views Thread by Donkeylung | last post: by
15 posts views Thread by fungus | last post: by
3 posts views Thread by toton | last post: by
5 posts views Thread by Jess | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.