473,403 Members | 2,222 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,403 software developers and data experts.

compilation error with forward declaration

Consider the following piece of code stored in a file called x.cpp

class Test;
extern void fn_one(Test t);
extern Test fn_two();

Test fn_three(const Test &obj)
{
fn_one(obj);

return fn_two();
}

Suppose I compile(only compile , not link) this program. I am getting
compilation error saying undefined use of class Test.

I do not understand why we cannot use a forward declared class in the
definition of a function but use it only in the function declaration.

Kindly explain

Thanks
V.Subramanian

Sep 17 '07 #1
3 2226
su**************@yahoo.com wrote:
Consider the following piece of code stored in a file called x.cpp

class Test;
extern void fn_one(Test t);
extern Test fn_two();

Test fn_three(const Test &obj)
{
fn_one(obj);

return fn_two();
}

Suppose I compile(only compile , not link) this program. I am getting
compilation error saying undefined use of class Test.

I do not understand why we cannot use a forward declared class in the
definition of a function but use it only in the function declaration.
Calling 'fn_one' requires the compiler to provide a call to the class
'Test's copy constructor. To know how to do that or whether it is at
all possible, the compiler has to have the 'Test' _definition_. What
if the copy constructor is private? Same requirement exists for the
return statement: a temporary is constructed from the return value of
'fn_two()' call -- and the constructability of that temporary here is
unknown unless you provide the definition of the 'Test' class.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 17 '07 #2
On Sep 17, 3:30 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Consider the following piece of code stored in a file called x.cpp

class Test;
extern void fn_one(Test t);
extern Test fn_two();

Test fn_three(const Test &obj)
{
fn_one(obj);
return fn_two();
}

Suppose I compile(only compile , not link) this program. I am getting
compilation error saying undefined use of class Test.

I do not understand why we cannot use a forward declared class in the
definition of a function but use it only in the function declaration.
You are telling the compiler to pass a copy of obj to fn_one, but
it can't copy something it knows nothing about.
Sep 17 '07 #3
In article <11**********************@w3g2000hsg.googlegroups. com>,
su**************@yahoo.com says...
Consider the following piece of code stored in a file called x.cpp

class Test;
extern void fn_one(Test t);
A forward declaration gives the compiler enough information to let you
create a pointer or a reference to an object of the forward declared
type, but not much more than that. It is NOT enough to allow you to
create objects of that type, and here you've defined the parameter to be
an object instead of a reference or pointer, so the compiler rejects the
code.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 18 '07 #4

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

Similar topics

6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
4
by: Piotr Sawuk | last post by:
I'm a newbie in the world of c++, and I am used to learn a programming language simply by programming. Unfortunately I where unable to find any useful helpfile for this language, in which such...
10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
10
by: Sune | last post by:
Hi, previously I used Eclipse CDT for compiling my files just to get started with C and leave C++ behind. Now it's time to get a little more serious so I've moved my files to a new workplace and...
3
by: DhaneshNair | last post by:
Hi all, I hav a file which is actually linkage file (used as an reference interface between c and c++ files). And this file has got two structures in it .. When i include this file directly i...
8
by: ftjonsson | last post by:
hello, I was wondering if anyone could tips me on what I am doing wrong when writing the following code: #include <iostream> using namespace std; int main () {
1
by: hamardk | last post by:
Hi, I'm trying to compile log4cpp-0.3.5rc3 on : SunOS belaurora 5.9 Generic_118558-11 sun4u sparc SUNW,Ultra-4 I'm having compilation issues which are: In file included from...
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
3
by: kanalkannan | last post by:
hi friends I compiled mpeg application and got some errors on that.. I used bfin-elf-g++ cross compiler Error: bitstrm.cpp:129: error: 'streampos' does not name a type bitstrm.cpp:135:...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
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...

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.