473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

forward declaration?

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 declaration?

class CEnemy : public CEntity
{
public:
CPlayer *player;
}

I tried it like this which brought the error:
"error: syntax error before `*' token"

class CEnemy : public CEntity
{
public:
// CPlayer *player;
CPlayer* player = new CPlayer;
}

THANKS for you help and regards.
Michael
Sep 27 '05 #1
3 20563
Michael Sgier wrote:
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?
No. It tells the compiler that CPlayer is a class. This is necessary if you
have classes that refer to each other, or you can do it in some
circumstances simply to avoid #including the header that defines the class,
which can speed up compilation.
// what's a forward declaration?
The declaration for CPlayer above is a forward declaration, i.e., you've
specified it as a class ahead of the full class definition, which will be
somewhere else.

class CEnemy : public CEntity
{
public:
CPlayer *player;
}
Missing semi-colon.

There must be other code that you compiled but did not post. Where is the
definition of CEntity? Also, to get an error for 'struct CPlayer' suggests
that CPlayer is really a struct, not a class. I'm guessing that the source
file you compiled #includes the full definition of CPlayer, which is a
struct, but the forward declaration says it's a class, hence the error
message.

You can use a forward declaration if you only refer to pointers or
references to CPlayer (which is the case here), but you need the entire
definition of CPlayer in other cases, such as defining a member object of
type CPlayer in CEnemy (as opposed to a pointer or reference), or accessing
any member of CPlayer, etc.

I tried it like this which brought the error:
"error: syntax error before `*' token"
Yes, because the compiler does not know what CPlayer is. You need a forward
declaration of CPlayer, the full definition of CPlayer, and you need the
full definitionof CEntity.
class CEnemy : public CEntity
{
public:
// CPlayer *player;
CPlayer* player = new CPlayer;
}


Another missing semi-colon.

DW
Sep 27 '05 #2
"David White" <no@email.provided> wrote in message
news:vJ******************@nasal.pacific.net.au...
Yes, because the compiler does not know what CPlayer is. You need a forward declaration of CPlayer,
Correction: Insert the word "or" here.
the full definition of CPlayer, and you need the
full definitionof CEntity.


DW

Sep 27 '05 #3
The following article demonstrates the use of forward declarations to
simplify header file management.

http://www.eventhelix.com/RealtimeMa...dePatterns.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Modeling Tool

Sep 27 '05 #4

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

Similar topics

6
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error:...
2
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying...
2
by: Legendary Pansy | last post by:
Hello, I'm trying to accomplish the impossible by trying to do something equivalent of this example found here http://www.c-sharpcorner.com/Code/2003/Dec/DialogTutorial.as Starting with "Listing...
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: toton | last post by:
Hi, I have two namespace contains class InkFrame and PrefDialog respectively. PrefDialog needs InkFrame to show the dialog over the frame. It also stores a pointer to InkFrame inside it. Now I...
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...
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
4
by: Steve | last post by:
Hi, I always though to return an instance of a class by value, it had to be defined - i.e. forward declaration isn't good enough? Consider the following code snippet: class RGBA; class...
7
by: RedLars | last post by:
Need some help with a couple of questions. Previously I have seen this declaration; struct Student { int age; char name; }; and this definition of an object; struct Student stud;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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...

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.