Forward Declaration produces an error (no cyclic dependencies tho)

E

elmar_macek

Hi all,

i have recently stumbled about a problem:

I have implemented a class GeneraBucket, which is superclass for
Bucket and PBucket. I need a method in PBucket that returns a pointer
to a Bucket:

Bucket* PBucket::convertToBucket()
{
return new Bucket(/*some values here*/);
}

Furthermore the Bucket class contains a pointer to a PBucket.

So i tried to solve the problem, that both classes need to know each
other by doing the following:




--- PBucket.hpp ---

//includes and stuff ...
#include "GeneralBucket.hpp"
class Bucket;

class PBucket:public GeneralBucket
{
//blabla
Bucket* convertToBucket();
//blabla
}

--- PBucket.cpp ---
#include PBucket.hpp


Bucket* PBucket::convertToBucket()
{
return new Bucket(/*some values here*/);
}


--- Bucket.hpp ---
#include "GeneralBucket.hpp"
#include "PBucket.hpp"

class Bucket:public GeneralBucket
{
//blabla
PBucket* upLeftPBuck;
//blabla
}

--- Bucket.cpp ---
//not important




Now i get the following error msges:

..../PBucket.cpp:42: error: invalid use of undefined type 'struct
Bucket'

and

..../PBucket.hpp:10: error: forward declaration of 'struct Bucket'


I really dont understand why, cause i include PBucket.hpp to
Bucket.hpp, which means that the compiler should find the proper
declaration after the forwarding one.

Thanks for your help in advance!
Sincerely
Björn
 
G

Guest

Hi all,

i have recently stumbled about a problem:

I have implemented a class GeneraBucket, which is superclass for
Bucket and PBucket. I need a method in PBucket that returns a pointer
to a Bucket:

Bucket* PBucket::convertToBucket()
{
return new Bucket(/*some values here*/);
}

Furthermore the Bucket class contains a pointer to a PBucket.

So i tried to solve the problem, that both classes need to know each
other by doing the following:




--- PBucket.hpp ---

//includes and stuff ...
#include "GeneralBucket.hpp"
class Bucket;

class PBucket:public GeneralBucket
{
//blabla
Bucket* convertToBucket();
//blabla
}

--- PBucket.cpp ---
#include PBucket.hpp


Bucket* PBucket::convertToBucket()
{
return new Bucket(/*some values here*/);
}


--- Bucket.hpp ---
#include "GeneralBucket.hpp"
#include "PBucket.hpp"

class Bucket:public GeneralBucket
{
//blabla
PBucket* upLeftPBuck;
//blabla
}

--- Bucket.cpp ---
//not important




Now i get the following error msges:

.../PBucket.cpp:42: error: invalid use of undefined type 'struct
Bucket'

and

.../PBucket.hpp:10: error: forward declaration of 'struct Bucket'


I really dont understand why, cause i include PBucket.hpp to
Bucket.hpp, which means that the compiler should find the proper
declaration after the forwarding one.

Is this what you want?

Bucket.hpp
----------
// Include guards

class PBucket;

class Bucket
{
PBucket* upLeftPBuck;
};

----------
Bucket.cpp
----------

#include "Bucket.hpp"
#include "PBucket.hpp"

// ....

-----------
PBucket.hpp
-----------
// Include guards

class Bucket;

class PBucket
{
public:
Bucket* convertToBucket();
};

-----------
PBucket.cpp
-----------

#include "PBucket.hpp"
#include "Bucket.hpp"

Bucket* PBucket::convertToBucket()
{
// ....
}
 
E

elmar_macek

@Erik
Cheers, Erik. That works perfectly for me. Never thought of including
more than <aClass>.hpp into <aClass>.cpp. Thanks alot! :)
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top