Connecting Tech Pros Worldwide Forums | Help | Site Map

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

for.fun@laposte.net
Guest
 
Posts: n/a
#1: Aug 18 '06
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 };
};


Victor Bazarov
Guest
 
Posts: n/a
#2: Aug 18 '06

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


for.fun@laposte.net wrote:
Quote:
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
Quote:
Quote:
>>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


mlimber
Guest
 
Posts: n/a
#3: Aug 18 '06

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


for....@laposte.net wrote:
Quote:
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.
Quote:
>
=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

for.fun@laposte.net
Guest
 
Posts: n/a
#4: Aug 18 '06

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


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 :
Quote:
for....@laposte.net wrote:
Quote:
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.
>
Quote:

=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
Alan Johnson
Guest
 
Posts: n/a
#5: Aug 18 '06

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



Alan Johnson wrote:
Quote:
for.fun@laposte.net wrote:
>
Quote:
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

Alan Johnson
Guest
 
Posts: n/a
#6: Aug 18 '06

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



for.fun@laposte.net wrote:
Quote:
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

for.fun@laposte.net
Guest
 
Posts: n/a
#7: Aug 21 '06

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


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

// 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) ;
} ;
Quote:
Sorry, I got the parameters to the constructors backwards, but you get
the idea.
for.fun@laposte.net
Guest
 
Posts: n/a
#8: Aug 21 '06

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


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

// 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) ;
} ;
Quote:
Sorry, I got the parameters to the constructors backwards, but you get
the idea.
Closed Thread