mutual class dependencies



 DEVELOP > c-Plus-Plus > mutual class dependencies

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "mosfet"
Date: 24 Jan 2008 09:00:38 AM
Object: mutual class dependencies
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'
.

User: "Alf P. Steinbach"

Title: Re: mutual class dependencies 24 Jan 2008 09:12:18 AM
* 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?
.
User: "mosfet"

Title: Re: mutual class dependencies 24 Jan 2008 09:18:01 AM
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.
.
User: "Alf P. Steinbach"

Title: Re: mutual class dependencies 24 Jan 2008 09:26:08 AM
* 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?
.
User: "jalina"

Title: Re: mutual class dependencies 24 Jan 2008 12:22:31 PM
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
.
User: "Chris Thomasson"

Title: Re: mutual class dependencies 24 Jan 2008 12:42:54 PM
"jalina" <jalina@nospam.please.com> wrote in message
news:4798d766$0$862$ba4acef3@news.orange.fr...
[...]

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

Don't you have any other ways to waste your time?
.

User: "Jack Klein"

Title: Re: mutual class dependencies 24 Jan 2008 11:24:12 PM
On Thu, 24 Jan 2008 19:22:31 +0100, jalina <jalina@nospam.please.com>
wrote in comp.lang.c++:

I have decided to boycott this newsgroup

Don't bother on my account.
*plonk*
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.

User: "Grizlyk"

Title: Re: mutual class dependencies 25 Jan 2008 08:43:11 PM
jalina wrote:


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

Do not pay any attention at the rancorous men, just because:
1. they cheafly do not give you any useful in C++ scope;
2. it is not easy for anybody (though i do not acquit any evil
behavior) to read and answer in a group, especially when there are
tons of questions, that can be easy answered without any questions by
posters itself.
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new
.

User: "anon"

Title: Re: mutual class dependencies 25 Jan 2008 03:24:22 AM
jalina wrote:

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

Cool. Start with nike sellers, free sex messages, free money sites, and
similar people.
.
User: "Alf P. Steinbach"

Title: Re: mutual class dependencies 25 Jan 2008 03:45:08 AM
* anon:

jalina wrote:

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


Cool. Start with nike sellers, free sex messages, free money sites, and
similar people.

Well, I think "jalina" had a point, although he/she may not have and
probably have not (yet) realized it.
Namely, that reading a technical discussion as if it were emotional
hinting and come-uppance (not that it isn't, sometimes!) is in general
very counter-productive. Technical accuracy is very important for
technical stuff. And as it happens, which is a bit ironic, that was my
message.
FAQ item 5.8 "How do I post a question about code that doesn't work
correctly", found at e.g. <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>, gives
some concrete advice about how to achieve technical accuracy when
posting, and hence ensuring more accurate answers and not wasting
other's time, and perhaps I should just have referred to that FAQ item
(I would if I'd known there was such a sensitive reader lurking! :-) ).
Cheers,
- 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?
.




User: "Phil Endecott"

Title: Re: mutual class dependencies 24 Jan 2008 10:42:03 AM

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

User: "Salt_Peter"

Title: Re: mutual class dependencies 24 Jan 2008 09:56:01 AM
On Jan 24, 10:18 am, mosfet <john....@anonymous.org> wrote:

Alf P. Steinbach a =E9crit :

* 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 =3D eContext;
return *this;
}


B::TContext m_eContext;
};


class B:
{
public:


enum TContext
{
EUserMessage =3D 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 =3D eContext;
return *this;
}

B::TContext m_eContext;

};

class B:
{
public:
enum TContext
{
EUserMessage =3D 0,
EWizzardQuestion,
EWizzardConfirmation,
EContextCount

};

protected:

A m_A;

}:

So I cannot do what you say.

=2E..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.
.

User: "Puppet_Sock"

Title: Re: mutual class dependencies 24 Jan 2008 10:35:40 AM
On Jan 24, 10:18=A0am, mosfet <john....@anonymous.org> wrote:

Alf P. Steinbach a =E9crit :



* mosfet:

Hi,


I would like to write something like :


//forward declarations
class B;
class A;


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


B::TContext m_eContext;
};


class B:
{
public:


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


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:
=A0 =A0 =A0A()
=A0 =A0 =A0{

=A0 =A0 =A0}

=A0 =A0 =A0A& SetContext(B::TContext eContext)
=A0 =A0 =A0{
=A0 =A0 =A0 =A0 =A0m_eContext =3D eContext;
=A0 =A0 =A0 =A0 =A0return *this;
=A0 =A0 =A0}

B::TContext m_eContext;

};

class B:
{
public:
enum TContext
{
EUserMessage =3D 0,
EWizzardQuestion,
EWizzardConfirmation,
EContextCount

};

protected:

A =A0m_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
.
User: "jalina"

Title: Re: mutual class dependencies 24 Jan 2008 12:22:23 PM
Puppet_Sock a écrit :

On Jan 24, 10:18 am, mosfet <john....@anonymous.org> 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
.
User: "James Connell"

Title: Re: mutual class dependencies 24 Jan 2008 08:13:23 PM
jalina wrote:


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

start now - you posted this message twice already.
.



User: "EventHelix.com"

Title: Re: mutual class dependencies 25 Jan 2008 08:38:47 PM
On Jan 24, 10:18=A0am, mosfet <john....@anonymous.org> wrote:

Alf P. Steinbach a =E9crit :

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:
=A0 =A0 =A0A()
=A0 =A0 =A0{

=A0 =A0 =A0}

=A0 =A0 =A0A& SetContext(B::TContext eContext)
=A0 =A0 =A0{
=A0 =A0 =A0 =A0 =A0m_eContext =3D eContext;
=A0 =A0 =A0 =A0 =A0return *this;
=A0 =A0 =A0}

B::TContext m_eContext;

};

class B:
{
public:
enum TContext
{
EUserMessage =3D 0,
EWizzardQuestion,
EWizzardConfirmation,
EContextCount

};

protected:

A =A0m_A;

}:

So I cannot do what you say.

Include A and B as references. That way you can use forward
declarations to take care of the cyclic dependencies.
The following article should help:
http://www.eventhelix.com/RealtimeMantra/HeaderFileIncludePatterns.htm
--
EventStudio 4.0 - http://www.Eventhelix.com/Eventstudio/
Sequence diagram based systems engineering tool
.

User: "Grizlyk"

Title: Re: mutual class dependencies 25 Jan 2008 08:19:52 PM
mosfet wrote:


//forward declarations
class B;
class A;


class A
{
public:
=A0 =A0 A()
=A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0}
=A0 =A0 =A0 =A0 A& SetContext(B::TContext eContext)


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


class B includes a A object:

It is evidently, you need TContext type completely declared, because
of you need to know sizeof(TContext) to pass eContext by value.
You can pass eContext by reference
A& SetContext(B::TContext& eContext)
or remove declaration of TContext into separated class
namespace NB{
class TContext
{
//
};
}
//forward declarations
class A;
class B;
//using NB::TContext;
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new
.
User: "Grizlyk"

Title: Re: mutual class dependencies 25 Jan 2008 10:15:47 PM
Grizlyk wrote:


//forward declarations
class B;
class A;


class A
{
public:
=A0 =A0 A()
=A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0}
=A0 =A0 =A0 =A0 A& SetContext(B::TContext eContext)


You can pass eContext by reference
A& SetContext(B::TContext& eContext)

Well, looks like useless advice here, because B::TContext must be
declared. You can not declare B::TContext like this (i do not know
why)
class B;
//using B::C;
//class B::C;
but you can use template<> to delay declaration
template<typename T=3DB>
class A
{
public:
A& SetContext(T::TContext& eContext);
};
template<typename T> //(T=3DB is not allowed)
void ::foo(T::TContext& c)
{
A<T> a;
a.SetContext(c);
}
class B
{
public:
//can be declared only for references
class TContext;
};
void ::boo(B::TContext& c)
{
foo<B>(c);
}

or remove declaration of TContext into separated class

And yet, instead of using B::TContext& reference, you can move body of
SetContext function outside of class A scope and place the body after
B::TContext complete declaration, but the declaration of SetContext
with eContext passed by value will touch you in code like this
void ::foo(B::TContext& b)
{
A a;
a.SetContext(b);
}
if the code ::foo will be placed befor B::TContext complete
declaration.
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new
.


User: "Pavel"

Title: Re: mutual class dependencies 28 Jan 2008 09:38:12 PM
mosfet 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.

That is an interesting problem, actually. Mixing in some templates
compiled for me (not that I enjoyed using templates for the wrong cause):
template<typename ThatEnum>
class A_GENERIC {
public: A_GENERIC() {}
A_GENERIC &
SetContext(ThatEnum eContext)
{
m_eContext = eContext;
return *this;
}
ThatEnum m_eContext;
};
class B
{
public:
enum TContext
{
EUserMessage = 0,
EWizzardQuestion,
EWizzardConfirmation,
EContextCount
};
protected:
A_GENERIC<B::TContext> m_A;
};
typedef A_GENERIC<B::TContext> A;
// at this point, we have regular (non-template) A, B and B::TContext as
ordered..
int main(int argc, char *argv[]) {
A a;
a.SetContext(B::EUserMessage);
B b;
return 0;
}
Hope this will help -- but I wonder if there can be a non-template solution.
-Pavel
.



User: ""

Title: Re: mutual class dependencies 24 Jan 2008 09:17:16 AM
On Jan 24, 10:00=A0am, mosfet <john....@anonymous.org> wrote:

Hi,

I would like to write something like :

//forward declarations
class B;
class A;

class A
{
public:
=A0 =A0 =A0 =A0 A()
=A0 =A0 =A0 =A0 {

=A0 =A0 =A0 =A0 }

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

B::TContext m_eContext;

};

class B:
{
public:

enum TContext
=A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 EUserMessage =3D 0,
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 EWizzardQuestion,
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 EWizzardConfirmation,
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 EContextCount
=A0 =A0 =A0 =A0 };

}:

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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER