Cannot call base function

Discussion in 'C++' started by none, May 3, 2010.

  1. none

    none Guest

    I have the following:

    class Base {
    public:
    void test();

    };

    class Sub : public Base {
    public:



    };



    int main() {

    Sub sub;
    sub.test();


    return 0;

    }

    which gives this error:


    main.cpp:(.text+0xa): undefined reference to `Base::test()'
    collect2: ld returned 1 exit status
    make[2]: *** [rus] Error 1
    make[1]: *** [CMakeFiles/rus.dir/all] Error 2
    make: *** [all] Error 2



    Why is it not possible to call the base function test() on the sub instance?
     
    none, May 3, 2010
    #1
    1. Advertising

  2. * none:
    > I have the following:
    >
    > class Base {
    > public:
    > void test();
    >
    > };
    >
    > class Sub : public Base {
    > public:
    >
    >
    >
    > };
    >
    >
    >
    > int main() {
    >
    > Sub sub;
    > sub.test();
    >
    >
    > return 0;
    >
    > }


    And where is your Base::test() ?

    >
    > which gives this error:
    >
    >
    > main.cpp:(.text+0xa): undefined reference to `Base::test()'
    > collect2: ld returned 1 exit status


    You never *defined* Base::test(), you merely declared it. Note that this
    is not an error from the compiler, but from the linker; since you didn't
    write any code for Base::test(), the linker obviously can't find it.

    --
    Martijn van Buul -
     
    Martijn van Buul, May 3, 2010
    #2
    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. tomek
    Replies:
    2
    Views:
    705
    Andrey Tarasevich
    Dec 1, 2003
  2. tomek
    Replies:
    3
    Views:
    789
    David Fisher
    Nov 30, 2003
  3. Firstname Lastname
    Replies:
    3
    Views:
    422
    Siemel Naran
    Feb 26, 2005
  4. grbgooglefan
    Replies:
    2
    Views:
    453
    Pascal Bourguignon
    Jan 30, 2008
  5. George2
    Replies:
    0
    Views:
    372
    George2
    Mar 17, 2008
Loading...

Share This Page