473,890 Members | 1,686 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 2778
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
1934
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
2241
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
1887
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
2855
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
2877
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
20623
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
2820
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
3880
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
5379
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
9976
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
9815
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10802
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...
1
10908
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9618
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
8008
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...
1
4665
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
3
3267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.