473,465 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Sep 26 '07 #1
2 1769
On 2007-09-26 13:40, el*********@gmx.de wrote:
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
Sep 26 '07 #2
@Erik
Cheers, Erik. That works perfectly for me. Never thought of including
more than <aClass>.hpp into <aClass>.cpp. Thanks alot! :)

Sep 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Brian Sabolik | last post by:
I'm not sure if I've broken any Object Oriented rules or not, but ... I have projects in 2 different solutions that need to use each other's methods. Therefore I may have an "update" method in...
5
by: Simon Elliott | last post by:
For some time I've been using typedefs for STL containers for reasons outlined in: http://www.gotw.ca/gotw/046.htm However a major downside to this is that you can't forward declare a typedef...
3
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward...
8
by: Heiner | last post by:
Hi! The following snippet class B; class A { public:
1
by: HappyHippy | last post by:
Hi, I have a question about forward declaration of classes. First of all, a simple working example, which compiles and works with no problems. ///file class_a.h:/ #ifndef __CLASS_A_H__...
1
by: xiaohuamao | last post by:
Beside declare independent abstract classes, is there any other methods to solve the problem of cyclic dependencies?Like the following one, which causes compile error of undefined type. class...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
5
by: fomas87 | last post by:
Hi guys, I'm writing a framework for an sdl gui in c++, but got stuck on the circular dependency, I've read a lot of articles about resolving dependencies and about forward declaration but none of...
5
by: aaragon | last post by:
Hello, I was wondering if it is possible to forward declare a type definition. If so, what is the way to do it? I found a couple of messages where they say it's not possible but there must be a way...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.