473,395 Members | 1,521 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,395 software developers and data experts.

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 2736
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(Node*);
};

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*****@bluegrass.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

"Randy Yates" <ya***@ieee.org> wrote in message
news:k6**********@ieee.org...
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.


One of the points of forward references is when the real definition is in a
different file and you don't want the compiler to have to compile the real
definition. This is useful in header files to cut down on dependencies
between header files.

john
Jul 22 '05 #11

"Randy Yates" <ya***@ieee.org> wrote in message
news:k6**********@ieee.org...
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.


This isn't an issue of compiler design, but language design.

Consider that C++ (like C) is designed to allow separate
compilation of modules of the same ultimate application.
Forward scan *which* file? (the needed one might not, and
often does not, yet exist).

-Mike
Jul 22 '05 #12

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

Similar topics

5
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...
10
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...
5
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...
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:...
7
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...
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...
11
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
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? ...
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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:
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
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...

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.