Reputation: 438
I have a small problem using virtual functions in C++
I have a class B which extends class A.
Class A{
virtual function 1 // does nothing
virtual function 2 // does nothing
}
class B : public class A {
function 1 { does some thing }
function 2 { does some thing }
}
I have another class implement
class implement {
B b;
A *a = &B;
a.function 1();
a.function 2();
}
This code when compiled gives the following error while compiling with GCC compiler
undefined reference to function 1 and function 2.
Please help me resolve this thanks in advance
Upvotes: 0
Views: 265
Reputation: 206526
In C++ only pure virtual functions are allowed to exist without a function definition.
In your code you do not have any pure virtual functions. Pure virtual functions are the one's which have an =0
in the declaration.
For example:
virtual void doSomething()=0;
The virtual
member functions(function1()
and function2()
) in your base class A
must have a definition since they are not pure virtual. You did not provide their definitions and hence the linker appropriately complains about missing definition.
undefined reference to function 1 and function 2
Upvotes: 5