compilation error with forward declaration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    #1

    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

  • Victor Bazarov

    #2
    Re: compilation error with forward declaration

    subramanian100i n@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


    Comment

    • tragomaskhalos

      #3
      Re: compilation error with forward declaration

      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.


      Comment

      • Jerry Coffin

        #4
        Re: compilation error with forward declaration

        In article <1190039431.465 131.139180@w3g2 000hsg.googlegr oups.com>,
        subramanian100i n@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.

        Comment

        Working...