Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > circular dependencies, forward doesn't help.

Reply
Thread Tools

circular dependencies, forward doesn't help.

 
 
rammel
Guest
Posts: n/a
 
      11-24-2006
class1.h
-----------
#ifndef CLASS1_H_
#define CLASS1_H_
#include "class2.h" // inclusion
class class2; // and forward

class class1
{
private:
class2 aRererence;
// --- gcc says: "error: field 'aRererence' has incomplete type" ---
public:
class1() :aReference( class2& aR ) {aReference = aR;};
// --- error according to the missing type of aReference ---
};
#endif /*CLASS1_H_*/

------------
class2.h
------------
#ifndef CLASS2_H_
#define CLASS2_H_
#include <vector>
#include "class1.h" // inclusion
class class1; // and forward
class class2
{
private:
std::vector<class1> someContainer;
public:
class2();
};
#endif /*CLASS2_H_*/
-----------------------------------------

What is the correct way around this error? I guess I could declare the
field as pointer, but I wanted to use the c++ reference mechanism.
Thanks for your help in advance.

 
Reply With Quote
 
 
 
 
Hooyoo
Guest
Posts: n/a
 
      11-24-2006

rammel wrote:
> class1.h
> -----------
> #ifndef CLASS1_H_
> #define CLASS1_H_
> #include "class2.h" // inclusion
> class class2; // and forward
>
> class class1
> {
> private:
> class2 aRererence;
> // --- gcc says: "error: field 'aRererence' has incomplete type" ---
> public:
> class1() :aReference( class2& aR ) {aReference = aR;};
> // --- error according to the missing type of aReference ---
> };
> #endif /*CLASS1_H_*/
>
> ------------
> class2.h
> ------------
> #ifndef CLASS2_H_
> #define CLASS2_H_
> #include <vector>
> #include "class1.h" // inclusion
> class class1; // and forward
> class class2
> {
> private:
> std::vector<class1> someContainer;
> public:
> class2();
> };
> #endif /*CLASS2_H_*/
> -----------------------------------------
>
> What is the correct way around this error? I guess I could declare the
> field as pointer, but I wanted to use the c++ reference mechanism.
> Thanks for your help in advance.


The same question I asked before:
"How to solve this problem?"
http://groups.google.com/group/comp....1f69a762f5a2ac

 
Reply With Quote
 
 
 
 
rammel
Guest
Posts: n/a
 
      11-24-2006

> The same question I asked before:
> "How to solve this problem?"
> http://groups.google.com/group/comp....1f69a762f5a2ac


Beeing a c++ newbie I have to admit that I was unable to get the point
from that discussion. So could you please be so kind and alter the
right line of my code?

I've read about the rules when to use forward or inclusion and tried
the most combinations without success.
I think I can't put out one of the includes because class1 holds an
vector of class2-elements and class2 needs a referece to the class1 it
sticks in.
I also have no clue how to redesign this without losing functionality.

 
Reply With Quote
 
Hooyoo
Guest
Posts: n/a
 
      11-24-2006

rammel wrote:
> class1.h
> -----------
> #ifndef CLASS1_H_
> #define CLASS1_H_
> #include "class2.h" // inclusion
> class class2; // and forward
>
> class class1
> {
> private:
> class2 aRererence;
> // --- gcc says: "error: field 'aRererence' has incomplete type" ---
> public:
> class1() :aReference( class2& aR );

Implement this method in your class1.cpp file, just declare it here.
> // --- error according to the missing type of aReference ---
> };
> #endif /*CLASS1_H_*/
>
> ------------
> class2.h
> ------------
> #ifndef CLASS2_H_
> #define CLASS2_H_
> #include <vector>
> #include "class1.h" // inclusion
> class class1; // and forward
> class class2
> {
> private:
> std::vector<class1> someContainer;
> public:
> class2();
> };
> #endif /*CLASS2_H_*/
> -----------------------------------------
>
> What is the correct way around this error? I guess I could declare the
> field as pointer, but I wanted to use the c++ reference mechanism.
> Thanks for your help in advance.


 
Reply With Quote
 
rammel
Guest
Posts: n/a
 
      11-24-2006

Hooyoo wrote:
> > class1() :aReference( class2& aR );

> Implement this method in your class1.cpp file, just declare it here.
> > // --- error according to the missing type of aReference ---



thank you again but that didn't help.
the main problem is already that declaration above:

class2 aRererence; // compiler says: "incomplete type"

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-24-2006
rammel wrote:

> class1.h
> -----------
> #ifndef CLASS1_H_
> #define CLASS1_H_
> #include "class2.h" // inclusion
> class class2; // and forward


why do you need both of these?


>
> class class1
> {
> private:
> class2 aRererence;
> // --- gcc says: "error: field 'aRererence' has incomplete type" ---
> public:
> class1() :aReference( class2& aR ) {aReference = aR;};
> // --- error according to the missing type of aReference ---
> };
> #endif /*CLASS1_H_*/
>
> ------------
> class2.h
> ------------
> #ifndef CLASS2_H_
> #define CLASS2_H_
> #include <vector>
> #include "class1.h" // inclusion
> class class1; // and forward
> class class2
> {
> private:
> std::vector<class1> someContainer;
> public:
> class2();
> };
> #endif /*CLASS2_H_*/
> -----------------------------------------
>
> What is the correct way around this error? I guess I could declare the
> field as pointer, but I wanted to use the c++ reference mechanism.


so declare it as a reference?

class2& aRererence;

Are you sure what you posted is your actual code?

> Thanks for your help in advance.



--
Nick Keighley

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-24-2006

Nick Keighley wrote:
> rammel wrote:
>
> > class1.h
> > -----------
> > #ifndef CLASS1_H_
> > #define CLASS1_H_
> > #include "class2.h" // inclusion
> > class class2; // and forward

>
> why do you need both of these?
>
>
> >
> > class class1
> > {
> > private:
> > class2 aRererence;
> > // --- gcc says: "error: field 'aRererence' has incomplete type" ---
> > public:
> > class1() :aReference( class2& aR ) {aReference = aR;};
> > // --- error according to the missing type of aReference ---
> > };
> > #endif /*CLASS1_H_*/
> >
> > ------------
> > class2.h
> > ------------
> > #ifndef CLASS2_H_
> > #define CLASS2_H_
> > #include <vector>
> > #include "class1.h" // inclusion
> > class class1; // and forward
> > class class2
> > {
> > private:
> > std::vector<class1> someContainer;
> > public:
> > class2();
> > };
> > #endif /*CLASS2_H_*/
> > -----------------------------------------
> >
> > What is the correct way around this error? I guess I could declare the
> > field as pointer, but I wanted to use the c++ reference mechanism.

>
> so declare it as a reference?
>
> class2& aRererence;
>
> Are you sure what you posted is your actual code?
>
> > Thanks for your help in advance.


try this:-

class1.h
-----------
#ifndef CLASS1_H_
#define CLASS1_H_
// #include "class2.h" *** REMOVED
class class2;

class class1
{
private:
class2& aRererence; // reference
public:
class1(class2& aR):aReference(aR) {}; // corrected
};


#endif /*CLASS1_H_*/

------------
class2.h
------------
#ifndef CLASS2_H_
#define CLASS2_H_
#include <vector>
#include "class1.h" // inclusion
// removed unnec. forward ref
class class2
{
private:
std::vector<class1> someContainer;
public:
class2();
};
#endif /*CLASS2_H_*/


I've not compiled this


--
Nick Keighley

 
Reply With Quote
 
rammel
Guest
Posts: n/a
 
      11-24-2006
i totally missed to use the & operator while declaring that field, but
declearing as reference was exactly what I wanted to do. thank you very
much!

(the constructor was false too but just a typo, that wasn't my actual
code, just an abstracted example)

 
Reply With Quote
 
iftekhar
Guest
Posts: n/a
 
      11-24-2006
You can try this, (did not compile )
class1.h
-----------
#ifndef CLASS1_H_
#define CLASS1_H_

class class2; // and forward
class class1
{
....
}
#include "class2.h" // inclusion

same for the "class2.h"

it should solve your problems

cheers
iftekhar


rammel wrote:
> class1.h
> -----------
> #ifndef CLASS1_H_
> #define CLASS1_H_
> #include "class2.h" // inclusion
> class class2; // and forward
>
> class class1
> {
> private:
> class2 aRererence;
> // --- gcc says: "error: field 'aRererence' has incomplete type" ---
> public:
> class1() :aReference( class2& aR ) {aReference = aR;};
> // --- error according to the missing type of aReference ---
> };
> #endif /*CLASS1_H_*/
>
> ------------
> class2.h
> ------------
> #ifndef CLASS2_H_
> #define CLASS2_H_
> #include <vector>
> #include "class1.h" // inclusion
> class class1; // and forward
> class class2
> {
> private:
> std::vector<class1> someContainer;
> public:
> class2();
> };
> #endif /*CLASS2_H_*/
> -----------------------------------------
>
> What is the correct way around this error? I guess I could declare the
> field as pointer, but I wanted to use the c++ reference mechanism.
> Thanks for your help in advance.


 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      11-24-2006

rammel skrev:
> class1.h
> -----------
> #ifndef CLASS1_H_
> #define CLASS1_H_
> #include "class2.h" // inclusion
> class class2; // and forward


There can never be a reason to both forwarddeclare and include the
declaration.
>
> class class1
> {
> private:
> class2 aRererence;

In this case when declaring the member by value, you do need the
complete type so gcc is right.

> // --- gcc says: "error: field 'aRererence' has incomplete type" ---


Because it does not see the complete definition of class2.
> public:
> class1() :aReference( class2& aR ) {aReference = aR;};
> // --- error according to the missing type of aReference ---
> };
> #endif /*CLASS1_H_*/
>
> ------------
> class2.h
> ------------
> #ifndef CLASS2_H_
> #define CLASS2_H_
> #include <vector>
> #include "class1.h" // inclusion
> class class1; // and forward
> class class2
> {
> private:
> std::vector<class1> someContainer;


Here the standard also requires a complete definition. So you can't
portably do what you want. The reason is that class2 (potentially)
contains a class1 that contains a class2 that contains a class1
that.... I believe you can se what these recursive definitions leads
to.
To solve it, yoy could e.g. look up pimpl.

/Peter
[snip]

 
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
forward class declaration and circular including blueblueblue2005 C++ 2 07-22-2005 02:31 PM
Semi-circular definitions (plus circular references) Kiuhnm C++ 16 01-03-2005 03:49 AM
Circular dependeny: Forward declaration does not help! Markus Dehmann C++ 6 11-01-2004 02:22 PM
Re-forward declaration of types which were already forward declared qazmlp C++ 1 02-15-2004 07:00 PM
Circular references are not supported Dan C Douglas ASP .Net 1 08-13-2003 09:21 PM



Advertisments