Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > mutual class dependencies

Reply
Thread Tools

mutual class dependencies

 
 
mosfet
Guest
Posts: n/a
 
      01-24-2008
Hi,

I would like to write something like :

//forward declarations
class B;
class A;


class A
{
public:
A()
{

}

A& SetContext(B::TContext eContext)
{
m_eContext = eContext;
return *this;
}

B::TContext m_eContext;
};


class B:
{
public:

enum TContext
{
EUserMessage = 0,
EWizzardQuestion,
EWizzardConfirmation,
EContextCount
};
}:

But When I try I Get error C2027: use of undefined type 'B'
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-24-2008
* mosfet:
> Hi,
>
> I would like to write something like :
>
> //forward declarations
> class B;
> class A;
>
>
> class A
> {
> public:
> A()
> {
>
> }
>
> A& SetContext(B::TContext eContext)
> {
> m_eContext = eContext;
> return *this;
> }
>
> B::TContext m_eContext;
> };
>
>
> class B:
> {
> public:
>
> enum TContext
> {
> EUserMessage = 0,
> EWizzardQuestion,
> EWizzardConfirmation,
> EContextCount
> };
> }:
>
> But When I try I Get error C2027: use of undefined type 'B'


Remove the forward declarations.

Move class B before class A.

Fix the syntax errors.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      01-24-2008
On Jan 24, 10:00*am, mosfet <(E-Mail Removed)> wrote:
> Hi,
>
> I would like to write something like :
>
> //forward declarations
> class B;
> class A;
>
> class A
> {
> public:
> * * * * A()
> * * * * {
>
> * * * * }
>
> * * * * A& SetContext(B::TContext eContext)
> * * * * {
> * * * * * * * * m_eContext = eContext;
> * * * * * * * * return *this;
> * * * * }
>
> B::TContext m_eContext;
>
> };
>
> class B:
> {
> public:
>
> enum TContext
> * * * * {
> * * * * * * * * EUserMessage = 0,
> * * * * * * * * EWizzardQuestion,
> * * * * * * * * EWizzardConfirmation,
> * * * * * * * * EContextCount
> * * * * };
>
> }:
>
> But When I try I Get error C2027: use of undefined type 'B'


You use B as a member of A so the compiler needs to have the
complete declaration of B.

You can include the B header file (instead of forward declaring
it). Also, possible solution is to have A contain a pointer to
B instead of a B.

HTH
 
Reply With Quote
 
mosfet
Guest
Posts: n/a
 
      01-24-2008
Alf P. Steinbach a écrit :
> * mosfet:
>> Hi,
>>
>> I would like to write something like :
>>
>> //forward declarations
>> class B;
>> class A;
>>
>>
>> class A
>> {
>> public:
>> A()
>> {
>> }
>> A& SetContext(B::TContext eContext)
>> {
>> m_eContext = eContext;
>> return *this;
>> }
>>
>> B::TContext m_eContext;
>> };
>>
>>
>> class B:
>> {
>> public:
>>
>> enum TContext
>> {
>> EUserMessage = 0,
>> EWizzardQuestion,
>> EWizzardConfirmation,
>> EContextCount
>> };
>> }:
>>
>> But When I try I Get error C2027: use of undefined type 'B'

>
> Remove the forward declarations.
>
> Move class B before class A.
>
> Fix the syntax errors.
>
>
> Cheers, & hth.,
>
> - Alf
>

Oups I forgot to say that class B includes a A object:


class A
{
public:
A()
{

}

A& SetContext(B::TContext eContext)
{
m_eContext = eContext;
return *this;
}

B::TContext m_eContext;
};



class B:
{
public:
enum TContext
{
EUserMessage = 0,
EWizzardQuestion,
EWizzardConfirmation,
EContextCount
};

protected:

A m_A;

}:


So I cannot do what you say.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-24-2008
* mosfet:
> Alf P. Steinbach a écrit :
>> * mosfet:
>>> Hi,
>>>
>>> I would like to write something like :
>>>
>>> //forward declarations
>>> class B;
>>> class A;
>>>
>>>
>>> class A
>>> {
>>> public:
>>> A()
>>> {
>>> }
>>> A& SetContext(B::TContext eContext)
>>> {
>>> m_eContext = eContext;
>>> return *this;
>>> }
>>>
>>> B::TContext m_eContext;
>>> };
>>>
>>>
>>> class B:
>>> {
>>> public:
>>>
>>> enum TContext
>>> {
>>> EUserMessage = 0,
>>> EWizzardQuestion,
>>> EWizzardConfirmation,
>>> EContextCount
>>> };
>>> }:
>>>
>>> But When I try I Get error C2027: use of undefined type 'B'

>>
>> Remove the forward declarations.
>>
>> Move class B before class A.
>>
>> Fix the syntax errors.
>>
>>
>> Cheers, & hth.,
>>
>> - Alf
>>

> Oups I forgot to say that class B includes a A object:
>
>
> class A
> {
> public:
> A()
> {
>
> }
>
> A& SetContext(B::TContext eContext)
> {
> m_eContext = eContext;
> return *this;
> }
>
> B::TContext m_eContext;
> };
>
>
>
> class B:
> {
> public:
> enum TContext
> {
> EUserMessage = 0,
> EWizzardQuestion,
> EWizzardConfirmation,
> EContextCount
> };
>
> protected:
>
> A m_A;
>
> }:
>
>
> So I cannot do what you say.


Oups I forgot to say that if the requirements change, change the
solution accordingly.


Cheers, & hth.

- Alf


PS: Don't forget to fix the syntax errors.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      01-24-2008
On Jan 24, 10:18 am, mosfet <(E-Mail Removed)> wrote:
> Alf P. Steinbach a écrit :
>
> > * mosfet:
> >> Hi,

>
> >> I would like to write something like :

>
> >> //forward declarations
> >> class B;
> >> class A;

>
> >> class A
> >> {
> >> public:
> >> A()
> >> {
> >> }
> >> A& SetContext(B::TContext eContext)
> >> {
> >> m_eContext = eContext;
> >> return *this;
> >> }

>
> >> B::TContext m_eContext;
> >> };

>
> >> class B:
> >> {
> >> public:

>
> >> enum TContext
> >> {
> >> EUserMessage = 0,
> >> EWizzardQuestion,
> >> EWizzardConfirmation,
> >> EContextCount
> >> };
> >> }:

>
> >> But When I try I Get error C2027: use of undefined type 'B'

>
> > Remove the forward declarations.

>
> > Move class B before class A.

>
> > Fix the syntax errors.

>
> > Cheers, & hth.,

>
> > - Alf

>
> Oups I forgot to say that class B includes a A object:
>
> class A
> {
> public:
> A()
> {
>
> }
>
> A& SetContext(B::TContext eContext)
> {
> m_eContext = eContext;
> return *this;
> }
>
> B::TContext m_eContext;
>
> };
>
> class B:
> {
> public:
> enum TContext
> {
> EUserMessage = 0,
> EWizzardQuestion,
> EWizzardConfirmation,
> EContextCount
>
> };
>
> protected:
>
> A m_A;
>
> }:
>
> So I cannot do what you say.


...unfortunately for you.
If type A has a member B and a type B has a member A (which has a B
which has an A .... ad infinitum) then the program has no way to
define concretely the resulting type.

However, you could have a type A as a member of type B and use a
pointer to B in type A.
 
Reply With Quote
 
Puppet_Sock
Guest
Posts: n/a
 
      01-24-2008
On Jan 24, 10:18*am, mosfet <(E-Mail Removed)> wrote:
> Alf P. Steinbach a écrit :
>
>
>
> > * mosfet:
> >> Hi,

>
> >> I would like to write something like :

>
> >> //forward declarations
> >> class B;
> >> class A;

>
> >> class A
> >> {
> >> public:
> >> * * A()
> >> * * {
> >> * * * * * *}
> >> * * * * A& SetContext(B::TContext eContext)
> >> * * {
> >> * * * * m_eContext = eContext;
> >> * * * * return *this;
> >> * * }

>
> >> B::TContext m_eContext;
> >> };

>
> >> class B:
> >> {
> >> public:

>
> >> enum TContext
> >> * * {
> >> * * * * EUserMessage = 0,
> >> * * * * EWizzardQuestion,
> >> * * * * EWizzardConfirmation,
> >> * * * * EContextCount
> >> * * };
> >> }:

>
> >> But When I try I Get error C2027: use of undefined type 'B'

>
> > Remove the forward declarations.

>
> > Move class B before class A.

>
> > Fix the syntax errors.

>
> > Cheers, & hth.,

>
> > - Alf

>
> Oups I forgot to say that class B includes a A object:
>
> class A
> {
> public:
> * * *A()
> * * *{
>
> * * *}
>
> * * *A& SetContext(B::TContext eContext)
> * * *{
> * * * * *m_eContext = eContext;
> * * * * *return *this;
> * * *}
>
> B::TContext m_eContext;
>
> };
>
> class B:
> {
> public:
> enum TContext
> {
> EUserMessage = 0,
> EWizzardQuestion,
> EWizzardConfirmation,
> EContextCount
>
> };
>
> protected:
>
> A *m_A;
>
> }:
>
> So I cannot do what you say.


And, of course, the world would come to an end if you moved
that enum out of class B.
Socks
 
Reply With Quote
 
Phil Endecott
Guest
Posts: n/a
 
      01-24-2008
> class A
> {
> public:
> A()
> {
>
> }
>
> A& SetContext(B::TContext eContext)
> {
> m_eContext = eContext;
> return *this;
> }
>
> B::TContext m_eContext;
> };
>
>
>
> class B:
> {
> public:
> enum TContext
> {
> EUserMessage = 0,
> EWizzardQuestion,
> EWizzardConfirmation,
> EContextCount
> };
>
> protected:
>
> A m_A;
>
> }:


You need to break the circular dependencies somewhere, and there are
several options. How about:

enum B_TContext { EUserMessage = 0 ...... };

class A {
A& SetContext(B_TContext eContent) ....
B_TContext m_eContext;
};

class B {
typedef B_TContext TContext;
A m_A;
};
 
Reply With Quote
 
jalina
Guest
Posts: n/a
 
      01-24-2008
Puppet_Sock a écrit :
> On Jan 24, 10:18 am, mosfet <(E-Mail Removed)> wrote:
>> Alf P. Steinbach a écrit :
>>
>>
>>
>>> * mosfet:
>>>> Hi,
>>>> I would like to write something like :
>>>> //forward declarations
>>>> class B;
>>>> class A;
>>>> class A
>>>> {
>>>> public:
>>>> A()
>>>> {
>>>> }
>>>> A& SetContext(B::TContext eContext)
>>>> {
>>>> m_eContext = eContext;
>>>> return *this;
>>>> }
>>>> B::TContext m_eContext;
>>>> };
>>>> class B:
>>>> {
>>>> public:
>>>> enum TContext
>>>> {
>>>> EUserMessage = 0,
>>>> EWizzardQuestion,
>>>> EWizzardConfirmation,
>>>> EContextCount
>>>> };
>>>> }:
>>>> But When I try I Get error C2027: use of undefined type 'B'
>>> Remove the forward declarations.
>>> Move class B before class A.
>>> Fix the syntax errors.
>>> Cheers, & hth.,
>>> - Alf

>> Oups I forgot to say that class B includes a A object:
>>
>> class A
>> {
>> public:
>> A()
>> {
>>
>> }
>>
>> A& SetContext(B::TContext eContext)
>> {
>> m_eContext = eContext;
>> return *this;
>> }
>>
>> B::TContext m_eContext;
>>
>> };
>>
>> class B:
>> {
>> public:
>> enum TContext
>> {
>> EUserMessage = 0,
>> EWizzardQuestion,
>> EWizzardConfirmation,
>> EContextCount
>>
>> };
>>
>> protected:
>>
>> A m_A;
>>
>> }:
>>
>> So I cannot do what you say.

>
> And, of course, the world would come to an end if you moved
> that enum out of class B.
> Socks


People answering in this newsgroup is so nice and well minded.

Everyone who is a newbye or not aware of usenet usage have such
"pleasant" answers from some people that are so clever (and certainly
think that the noobs posting dumb....s only deserve contempt - just read
their "nice" answers)

Perhaps rename this newgroup to something like comp.land.advanced.c++ so
that noobs don't get to post there. I guess that between clever people,
contempt does not matter.

I have decided to boycott this newsgroup and I am going to start an
campaign to inform people not to read and post here.

Jalina
 
Reply With Quote
 
jalina
Guest
Posts: n/a
 
      01-24-2008
Alf P. Steinbach a écrit :
> * mosfet:
>> Alf P. Steinbach a écrit :
>>> * mosfet:
>>>> Hi,
>>>>
>>>> I would like to write something like :
>>>>
>>>> //forward declarations
>>>> class B;
>>>> class A;
>>>>
>>>>
>>>> class A
>>>> {
>>>> public:
>>>> A()
>>>> {
>>>> }
>>>> A& SetContext(B::TContext eContext)
>>>> {
>>>> m_eContext = eContext;
>>>> return *this;
>>>> }
>>>>
>>>> B::TContext m_eContext;
>>>> };
>>>>
>>>>
>>>> class B:
>>>> {
>>>> public:
>>>>
>>>> enum TContext
>>>> {
>>>> EUserMessage = 0,
>>>> EWizzardQuestion,
>>>> EWizzardConfirmation,
>>>> EContextCount
>>>> };
>>>> }:
>>>>
>>>> But When I try I Get error C2027: use of undefined type 'B'
>>>
>>> Remove the forward declarations.
>>>
>>> Move class B before class A.
>>>
>>> Fix the syntax errors.
>>>
>>>
>>> Cheers, & hth.,
>>>
>>> - Alf
>>>

>> Oups I forgot to say that class B includes a A object:
>>
>>
>> class A
>> {
>> public:
>> A()
>> {
>>
>> }
>>
>> A& SetContext(B::TContext eContext)
>> {
>> m_eContext = eContext;
>> return *this;
>> }
>>
>> B::TContext m_eContext;
>> };
>>
>>
>>
>> class B:
>> {
>> public:
>> enum TContext
>> {
>> EUserMessage = 0,
>> EWizzardQuestion,
>> EWizzardConfirmation,
>> EContextCount
>> };
>>
>> protected:
>>
>> A m_A;
>>
>> }:
>>
>>
>> So I cannot do what you say.

>
> Oups I forgot to say that if the requirements change, change the
> solution accordingly.
>
>
> Cheers, & hth.
>
> - Alf
>
>
> PS: Don't forget to fix the syntax errors.
>
>

People answering in this newsgroup is so nice and well minded.

Everyone who is a newbye or not aware of usenet usage have such
"pleasant" answers from some people that are so clever (and certainly
think that the noobs posting dumb....s only deserve contempt - just read
their "nice" answers)

Perhaps rename this newgroup to something like comp.land.advanced.c++ so
that noobs don't get to post there. I guess that between clever people,
contempt does not matter.

I have decided to boycott this newsgroup and I am going to start an
campaign to inform people not to read and post here.

Jalina
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exercise: how to remove mutual dependencies ? Anonimo C++ 8 03-16-2011 07:32 PM
ISIS and EIGRP mutual redistribution --Help! phillip Cisco 2 09-19-2008 08:27 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Problems mutual redistributing of ISIS and EIGRP phillip Cisco 5 12-07-2004 07:49 PM
Mutual EJB possible? Vjeran Marcinko Java 3 10-17-2003 09:21 PM



Advertisments