Re: linker error in virtual function
Rahul wrote:
Hi Everyone,
I have the following polymorphic classes,
class Shape
{
public : virtual void draw()
{
}
Trying to write Java are we :)
virtual void sample();
If you intend this to be pure virtual (no body defined) it should be
virtual void sample() = 0;
};
class Circle : public Shape
{
public : virtual void draw()
{
cout<<"Circle::draw"<<endl;
}
You must have a sample() method here if it is pure virtual in Shape.
};
int main()
{
Shape *ptr = new Circle;
ptr->draw();
return(0);
return isn't a function.
}
and i get a linker error saying undefined external sumbol __sample...
But i haven't really invoked the function sample(). Note that the code
works fine when i change sample() to a non-virtual function prototype,
why is this the case?
Virtual function have to be defined in order to build the objects
virtual function table.
--
Ian Collins.