compilation error with forward declaration

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Sep 17, 2007.

  1. , India

    , India Guest

    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
    , India, Sep 17, 2007
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Sep 17, 2007
    #2
    1. Advertising

  3. On Sep 17, 3:30 pm, ", India"
    <> 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.


    You are telling the compiler to pass a copy of obj to fn_one, but
    it can't copy something it knows nothing about.
    tragomaskhalos, Sep 17, 2007
    #3
  4. , India

    Jerry Coffin Guest

    In article <>,
    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.
    Jerry Coffin, Sep 18, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    956
  2. qazmlp
    Replies:
    1
    Views:
    598
    Jonathan Turkanis
    Feb 15, 2004
  3. ÂËÉ«¾µ
    Replies:
    3
    Views:
    3,198
    benben
    Dec 6, 2006
  4. Replies:
    2
    Views:
    478
  5. C__chp
    Replies:
    4
    Views:
    503
    Puppet_Sock
    Feb 15, 2008
Loading...

Share This Page