473,941 Members | 10,221 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2252
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 constructabilit y 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.comwro te:
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************ **********@w3g2 000hsg.googlegr oups.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
5234
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 TESTDRIVER_HPP #include <ostream> namespace ns_testdriver{ using std::ostream;
4
2788
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 basic things as "class", "operator ::", or even my compiler's error-messages would get explained in a quick-reference kind of way (as turbo pascal did with the pascal-language). Therefore I was forced to read some books and tutorials. However, I'm...
10
3536
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 struct foobar; ^
10
2378
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 begun to use GNU Autotools. I'm sorry to say I'm new to gcc as well :( Now I get the most ridiculous compile error which I'm unable to solve. Can someone, please, help me with this? gcc output together with the files mentioned in the gcc error...
3
4116
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 dont have any issues. but according to some norms in the review I am not supposed to use this file direclty in the header file .. but its fine if i use that inside the corresponding file. To avoid compiler error, i forward declared the structure...
8
3188
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
2987
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 PortabilityImpl.hh:12, from Appender.cpp:10: .../include/log4cpp/Portability.hh:40: syntax error before `('
1
2828
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 incorporate the same logic within my simulation, the class that implements the functor logic has problems compiling. I get the following errors: -- Building myTest.cpp --
3
2681
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: error: 'strmpos' was not declared in this scope bitstrm.cpp:135: error: invalid use of undefined type 'struct istream'
0
10134
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
9964
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
11530
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
11113
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
11296
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
9858
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...
0
7389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6298
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4908
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

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.