circular dependencies, forward doesn't help.

R

rammel

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_*/
 
H

Hooyoo

rammel said:
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...3d770/5f1f69a762f5a2ac?hl=en#5f1f69a762f5a2ac
 
R

rammel


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

Hooyoo

rammel said:
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.
 
R

rammel

Hooyoo said:
Implement this method in your class1.cpp file, just declare it here.


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

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

Nick Keighley

rammel said:
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_*/

so declare it as a reference?

class2& aRererence;

Are you sure what you posted is your actual code?
 
N

Nick Keighley

Nick said:
why do you need both of these?



so declare it as a reference?

class2& aRererence;

Are you sure what you posted is your actual code?

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
 
R

rammel

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)
 
I

iftekhar

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
 
P

peter koch

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]
 

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,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top