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

F

for.fun

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 };
};
 
V

Victor Bazarov

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

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
 
M

mlimber

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
 
F

for.fun

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 :
 
A

Alan Johnson

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) ;
} ;
 
A

Alan Johnson

Alan said:
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.
 
F

for.fun

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<A> Ptr;
};

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

#include "A.h"

class A;

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

.... 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 :
 
F

for.fun

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<A> Ptr;
};

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

#include "A.h"

class A;

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


.... 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 :
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top