compilation error with forward declaration

S

subramanian100in

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
 
V

Victor Bazarov

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
 
T

tragomaskhalos

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.
 
J

Jerry Coffin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top