473,656 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forward References

I'm having a problem with forward references.

For example,

class DATE;
class MYCLASS;

class MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int day;
int month;
int year;
};

int main(int argc, char* argv)
{
return 0;
}
results in
g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
impmain.cpp:8: field `date' has incomplete type
make: *** [pc/impmain.o] Error 1

Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
Why? I've declared the forward reference at the beginning of the file. Is
this just a stupidity of g++??
--
% Randy Yates % "Watching all the days go by...
%% Fuquay-Varina, NC % Who are you and who am I?"
%%% 919-577-9882 % 'Mission (A World Record)',
%%%% <ya***@ieee.org > % *A New World Record*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #1
11 2760
Randy Yates <ya***@ieee.org > writes:
I'm having a problem with forward references.

For example,

class DATE;
class MYCLASS;

class MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int day;
int month;
int year;
};

int main(int argc, char* argv)
{
return 0;
}
results in
g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
impmain.cpp:8: field `date' has incomplete type
make: *** [pc/impmain.o] Error 1

Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
Why? I've declared the forward reference at the beginning of the file. Is
this just a stupidity of g++??


I forgot to add that if I switch the order of the class declarations
the error goes away. I shouldn't have to order class declarations
which have forward references in my files, just as I don't have to
order subroutines if they have prototype definitions.
--
% Randy Yates % "The dreamer, the unwoken fool -
%% Fuquay-Varina, NC % in dreams, no pain will kiss the brow..."
%%% 919-577-9882 %
%%%% <ya***@ieee.org > % 'Eldorado Overture', *Eldorado*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #2
Randy Yates posted:
I'm having a problem with forward references.

For example,

class DATE;
class MYCLASS;

class MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int day;
int month;
int year;
};

int main(int argc, char* argv)
{
return 0;
}

Rearrange it:

(Plus I corrected a typo, it should've been "argv[]")
class DATE
{
public:
int day;
int month;
int year;
};

class MYCLASS
{
public:
int n;
DATE date;
};
int main(int argc, char* argv[])
{
return 0;
}
-JKop
Jul 22 '05 #3
Randy Yates wrote:
Why? I've declared the forward reference at the beginning of the file. Is
this just a stupidity of g++??


Forward declaration allow only to declare pointer or references, to declare
an object of the class the complete declaration is required.

--
Salu2
Jul 22 '05 #4

"Randy Yates" <ya***@ieee.org > wrote in message
news:u0******** **@ieee.org...
I'm having a problem with forward references.

For example,

class DATE;
class MYCLASS;

class MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int day;
int month;
int year;
};

int main(int argc, char* argv)
{
return 0;
}
results in
g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I
/devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
impmain.cpp:8: field `date' has incomplete type
make: *** [pc/impmain.o] Error 1

Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
Why? I've declared the forward reference at the beginning of the f ile.Is
this just a stupidity of g++??


C++ is no different from C in this regard. Replace class with struct, remove
'public:', and you'd have a C program and exactly the same error. You are a
C programmer, no?

In order to compile MYCLASS the compiler needs some basic information, such
as the size of each data member. It cannot get that from a forward
declaration.

class DATE;

class MYCLASS
{
public:
int n;
DATE* date;
};

This is OK because DATE* has a known size; but what you wrote is not because
the compiler does not know the size of DATE.

Suggest you simply reorder the classes.

john
Jul 22 '05 #5


Randy Yates wrote:
I'm having a problem with forward references.

For example,

class DATE;
class MYCLASS;

class MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int day;
int month;
int year;
};

int main(int argc, char* argv)
{
return 0;
}

results in

g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
impmain.cpp:8: field `date' has incomplete type
make: *** [pc/impmain.o] Error 1

Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27

Why? I've declared the forward reference at the beginning of the file. Is
this just a stupidity of g++??
--
% Randy Yates % "Watching all the days go by...
%% Fuquay-Varina, NC % Who are you and who am I?"
%%% 919-577-9882 % 'Mission (A World Record)',
%%%% <ya***@ieee.org > % *A New World Record*, ELO
http://home.earthlink.net/~yatescr


when the compiler is compiling 'myclass' it runs into a forward declared reference to 'date'. However, while date is known to
exist, the compiler does not know anything about it, such as size or layout. But at this phase of the compilation process, the
compiler needs to know at *least* the size, and possibly more so that it can complete the declaration of myclass. That means that
to use date here, you had to have previously done more than forward declaring it.

The forward declarations are primarily used to declare types that are going to be used as pointer references
in subsequent classes, this way you can have classes that point to each other in a circular fashion.

David
Jul 22 '05 #6
"John Harrison" <jo************ *@hotmail.com> writes:
"Randy Yates" <ya***@ieee.org > wrote in message
news:u0******** **@ieee.org...
I'm having a problem with forward references.

For example,

class DATE;
class MYCLASS;

class MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int day;
int month;
int year;
};

int main(int argc, char* argv)
{
return 0;
}
results in
g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I
/devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
impmain.cpp:8: field `date' has incomplete type
make: *** [pc/impmain.o] Error 1

Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
Why? I've declared the forward reference at the beginning of the f ile.Is
this just a stupidity of g++??


C++ is no different from C in this regard. Replace class with struct, remove
'public:', and you'd have a C program and exactly the same error. You are a
C programmer, no?


Yes, and structure definitions have no such thing as a "forward reference," and
if they did I'd be asking the same question.

If a forward reference isn't good for this, then what is it good for?
--
% Randy Yates % "...the answer lies within your soul
%% Fuquay-Varina, NC % 'cause no one knows which side
%%% 919-577-9882 % the coin will fall."
%%%% <ya***@ieee.org > % 'Big Wheels', *Out of the Blue*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #7

"Randy Yates" <ya***@ieee.org > wrote in message
news:3c******** **@ieee.org...
"John Harrison" <jo************ *@hotmail.com> writes:
"Randy Yates" <ya***@ieee.org > wrote in message
news:u0******** **@ieee.org...
I'm having a problem with forward references.

For example,

class DATE;
class MYCLASS;

class MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int day;
int month;
int year;
};

int main(int argc, char* argv)
{
return 0;
}
results in
g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I
/devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
impmain.cpp:8: field `date' has incomplete type
make: *** [pc/impmain.o] Error 1

Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
Why? I've declared the forward reference at the beginning of the f ile.Is this just a stupidity of g++??
C++ is no different from C in this regard. Replace class with struct, remove 'public:', and you'd have a C program and exactly the same error. You are a C programmer, no?


Yes, and structure definitions have no such thing as a "forward

reference,"

Nor do class definitions. But in C, yes a forward reference to
a struct can indeed be declared.

The following is valid C (as well as C++):

struct x; /* forward declare type 'struct x' */
struct x *p; /* define a pointer to type 'struct x' */

/* (but an actual object of type 'struct x' cannot be
created until after type 'struct x' is completely defined) */

struct x s; /* invalid */

/* fully define type 'struct x' */
struct x
{
int a;
};

struct x s2; /* valid */
and
if they did I'd be asking the same question.

If a forward reference isn't good for this, then what is it good for?


For defining a pointer (or reference in C++) to a struct or class which
has yet to be defined.

This issue works exactly the same way in C as in C++

-Mike

Jul 22 '05 #8
>>
C++ is no different from C in this regard. Replace class with struct,
remove
'public:', and you'd have a C program and exactly the same error. You are
a
C programmer, no?
Yes, and structure definitions have no such thing as a "forward
reference," and
if they did I'd be asking the same question.


Not true.

struct Node;

That is perfectly good C and C++.
If a forward reference isn't good for this, then what is it good for?


It tells the compiler that a name is the name of a class (or struct). You
can do a few things with just a name, for instance you can declare a pointer
or a reference. For instance

struct Node;

class List
{
public:
void add_node(Node*) ;
void remove_node(Nod e*);
};

John
Jul 22 '05 #9
Thanks David, John, and Mike. I guess I see the point now, but
I don't see why the compiler couldn't have been designed to
forward scan the file and pick up that information without
the user having to go through these hoops - something like
a "two-pass" compiler design.

--RY
David Lindauer <ca*****@bluegr ass.net> writes:
Randy Yates wrote:
I'm having a problem with forward references.

For example,

class DATE;
class MYCLASS;

class MYCLASS
{
public:
int n;
DATE date;
};

class DATE
{
public:
int day;
int month;
int year;
};

int main(int argc, char* argv)
{
return 0;
}

results in

g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
impmain.cpp:8: field `date' has incomplete type
make: *** [pc/impmain.o] Error 1

Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27

Why? I've declared the forward reference at the beginning of the file. Is
this just a stupidity of g++??
--
% Randy Yates % "Watching all the days go by...
%% Fuquay-Varina, NC % Who are you and who am I?"
%%% 919-577-9882 % 'Mission (A World Record)',
%%%% <ya***@ieee.org > % *A New World Record*, ELO
http://home.earthlink.net/~yatescr


when the compiler is compiling 'myclass' it runs into a forward declared reference to 'date'. However, while date is known to
exist, the compiler does not know anything about it, such as size or layout. But at this phase of the compilation process, the
compiler needs to know at *least* the size, and possibly more so that it can complete the declaration of myclass. That means that
to use date here, you had to have previously done more than forward declaring it.

The forward declarations are primarily used to declare types that are going to be used as pointer references
in subsequent classes, this way you can have classes that point to each other in a circular fashion.

David


--
% Randy Yates % "My Shangri-la has gone away, fading like
%% Fuquay-Varina, NC % the Beatles on 'Hey Jude'"
%%% 919-577-9882 %
%%%% <ya***@ieee.org > % 'Shangri-La', *A New World Record*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #10

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

Similar topics

5
1919
by: Thomas Matthews | last post by:
Hi, I have a Display class. I would like to write a function that takes a range of objects and displays them. The range would be specified by two forward iterators: start and end (one past start). I created a base class "References" to test this concept. I want the display function to process either a vector<References> or a list<References>. However, in my compiler (Borland C++ Builder), the std::list has a different iterator...
10
2221
by: Alan Lee | last post by:
Hi i am writing a small simulation for a bunch of atoms jumping around on a surface. I know i am a crappy programmer since i am not very familiar with object oriented languages. I am woindering if anyone can tell me why my forward class declarations not working. the member function findpaths can't seem to access the class Board when I try to pass it a board object by value. I put the Board class latrerbut also put in a forward...
5
1872
by: John Gabriele | last post by:
I'm hoping someone can please help me remember the C++ rule: When you're writing a header file for a class (say, some_namespace::Bar), and that class makes use of another class (some_namespace::Foo), -------------------------------- snip -------------------------------- #ifndef GUARD_Foo_HPP #define GUARD_Foo_HPP namespace some_namespace {
6
2844
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: "field `foo' has incomplete type" for the following code: #include <iostream> #include <vector>
7
2861
by: Lynn | last post by:
I am rewriting some memory management code and I need to have a forward declaration of a data structure. I am not converting this data structure into a class (yet). How do I generate a forward reference of a data structure ? For a class, I just say: class SomeClass; "struct SomeStructure;" does not work to forward declare the data structure. This is the structure that I am trying to generate a vector of pointers to:
3
20596
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 declaration? class CEnemy : public CEntity { public:
11
2804
by: Milind | last post by:
Hi, I was trying to implement a composition relation, somthing of the following type: class A { public: class B {
23
3841
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
4
5364
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 Colour
0
8382
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8600
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7311
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.