Connecting Tech Pros Worldwide Help | Site Map

Forward Declaration produces an error (no cyclic dependencies tho)

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 26th, 2007, 11:45 AM
elmar_macek@gmx.de
Guest
 
Posts: n/a
Default Forward Declaration produces an error (no cyclic dependencies tho)

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


  #2  
Old September 26th, 2007, 12:25 PM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
Default Re: Forward Declaration produces an error (no cyclic dependenciestho)

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
  #3  
Old September 26th, 2007, 12:55 PM
elmar_macek@gmx.de
Guest
 
Posts: n/a
Default 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! :)

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.