Cannot call base function

N

none

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?
 
M

Martijn van Buul

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

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

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top