Connecting Tech Pros Worldwide Forums | Help | Site Map

Forward Declaration produces an error (no cyclic dependencies tho)

elmar_macek@gmx.de
Guest
 
Posts: n/a
#1: Sep 26 '07
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


=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#2: Sep 26 '07

re: Forward Declaration produces an error (no cyclic dependencies tho)


On 2007-09-26 13:40, elmar_macek@gmx.de wrote:
Quote:
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()
{
// ....
}

--
Erik Wikström
elmar_macek@gmx.de
Guest
 
Posts: n/a
#3: Sep 26 '07

re: Forward Declaration produces an error (no cyclic dependencies tho)


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

Closed Thread