Abstract class linking issue

Discussion in 'C++' started by Richard, Oct 11, 2006.

  1. Richard

    Richard Guest

    First question - let's get this out of the way since it might be the
    solution to all my woes: Does it make sense to have a .cpp file for a
    class that is declared as having pure virtual functions in its .h file?

    Here's my predicament. I'll put this in general terms since it's
    happening across several classes.

    I have a base class, let's say it's named A. In A.h I DECLARE several
    pure virtual ( = 0) functions, a virtual destructor, and a constructor.
    Note that each of these is terminated with a semicolon, hence they're
    not DEFINED. There is also a file A.cpp which defines the constructor
    and destructor with empty curly braces. A.cpp and A.h compile into a
    library file (.a) with GCC without errors.

    There's a class B that derives class A. All of the pure virtual
    functions are overridden by B. Class B also has a default constructor
    (no parameters) and destructor. Class B compiles into a library
    without error also.

    When I go to link my executable I include the two resulting libraries.
    The linker produces the error "undefined reference to A::A()" which is
    said to occur in the constructor for class B. I've ensured that the
    folders containing A.h and B.h are in the "include search path", and
    I've ensured that the libraries containing A and B are included during
    linking.
    Richard, Oct 11, 2006
    #1
    1. Advertising

  2. Richard wrote:
    > First question - let's get this out of the way since it might be the
    > solution to all my woes: Does it make sense to have a .cpp file for a
    > class that is declared as having pure virtual functions in its .h
    > file?


    Sure, why not? If you want to provide definitions for the pure virtual
    functions, you may. It's especially useful if you ever call any of the
    functions from a c-tor of that class.

    > Here's my predicament. I'll put this in general terms since it's
    > happening across several classes.
    >
    > I have a base class, let's say it's named A. In A.h I DECLARE several
    > pure virtual ( = 0) functions, a virtual destructor, and a
    > constructor. Note that each of these is terminated with a semicolon,
    > hence they're not DEFINED. There is also a file A.cpp which defines
    > the constructor and destructor with empty curly braces. A.cpp and
    > A.h compile into a library file (.a) with GCC without errors.


    OK. Just so we are straight here, "compiles into a library" is not
    defined in C++.

    > There's a class B that derives class A. All of the pure virtual
    > functions are overridden by B. Class B also has a default constructor
    > (no parameters) and destructor. Class B compiles into a library
    > without error also.


    OK

    > When I go to link my executable I include the two resulting libraries.


    Linking with libraries isn't defined either. It's known to work in
    many compilers, but on every compiler that operation is specific to
    the implemenation.

    > The linker produces the error "undefined reference to A::A()" which is
    > said to occur in the constructor for class B. I've ensured that the
    > folders containing A.h and B.h are in the "include search path", and
    > I've ensured that the libraries containing A and B are included during
    > linking.


    Does it happen if you add all those files to the same project instead
    of using "resulting libraries"? If it does not happen, then your
    problem is not of C++ nature, sorry. If it does happen even if your
    classes are both parts of the same _source_ file, let's see it.

    For implementation-specific behaviour you need to ask in the newsgroup
    dedicated to your compiler.

    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, Oct 11, 2006
    #2
    1. Advertising

  3. Richard

    Pete Becker Guest

    Victor Bazarov wrote:
    > Richard wrote:
    >> First question - let's get this out of the way since it might be the
    >> solution to all my woes: Does it make sense to have a .cpp file for a
    >> class that is declared as having pure virtual functions in its .h
    >> file?

    >
    > Sure, why not? If you want to provide definitions for the pure virtual
    > functions, you may. It's especially useful if you ever call any of the
    > functions from a c-tor of that class.
    >
    >> Here's my predicament. I'll put this in general terms since it's
    >> happening across several classes.
    >>
    >> I have a base class, let's say it's named A. In A.h I DECLARE several
    >> pure virtual ( = 0) functions, a virtual destructor, and a
    >> constructor. Note that each of these is terminated with a semicolon,
    >> hence they're not DEFINED. There is also a file A.cpp which defines
    >> the constructor and destructor with empty curly braces. A.cpp and
    >> A.h compile into a library file (.a) with GCC without errors.

    >
    > OK. Just so we are straight here, "compiles into a library" is not
    > defined in C++.
    >
    >> There's a class B that derives class A. All of the pure virtual
    >> functions are overridden by B. Class B also has a default constructor
    >> (no parameters) and destructor. Class B compiles into a library
    >> without error also.

    >
    > OK
    >
    >> When I go to link my executable I include the two resulting libraries.

    >
    > Linking with libraries isn't defined either. It's known to work in
    > many compilers, but on every compiler that operation is specific to
    > the implemenation.
    >
    >> The linker produces the error "undefined reference to A::A()" which is
    >> said to occur in the constructor for class B. I've ensured that the
    >> folders containing A.h and B.h are in the "include search path", and
    >> I've ensured that the libraries containing A and B are included during
    >> linking.

    >
    > Does it happen if you add all those files to the same project instead
    > of using "resulting libraries"?


    "add all those files to the same project" is not defined in C++.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
    Pete Becker, Oct 11, 2006
    #3
  4. Pete Becker wrote:
    > Victor Bazarov wrote:
    >> Richard wrote:

    [...]
    >>> The linker produces the error "undefined reference to A::A()" which
    >>> is said to occur in the constructor for class B. I've ensured that
    >>> the folders containing A.h and B.h are in the "include search
    >>> path", and I've ensured that the libraries containing A and B are
    >>> included during linking.

    >>
    >> Does it happen if you add all those files to the same project instead
    >> of using "resulting libraries"?

    >
    > "add all those files to the same project" is not defined in C++.


    Something crawled up somewhere this fine morning, eh?

    Replace 'project' with 'program'.

    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, Oct 11, 2006
    #4
  5. Richard

    Richard Guest

    It turned out to be a linking order issue. I modified the order in my
    Makefile and resolved all the "undefined reference" errors.
    Richard, Oct 12, 2006
    #5
    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. Matthias Kaeppler
    Replies:
    1
    Views:
    435
    R.F. Pels
    May 22, 2005
  2. Sameer
    Replies:
    4
    Views:
    584
    Roedy Green
    Aug 31, 2005
  3. Uzytkownik
    Replies:
    3
    Views:
    589
    Uzytkownik
    Apr 3, 2005
  4. Iyer, Prasad C

    Abstract Methods & Abstract Class

    Iyer, Prasad C, Oct 20, 2005, in forum: Python
    Replies:
    0
    Views:
    530
    Iyer, Prasad C
    Oct 20, 2005
  5. Replies:
    4
    Views:
    809
    Rolf Magnus
    May 17, 2006
Loading...

Share This Page