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

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top