linker error in virtual function

R

Rahul

Hi Everyone,

I have the following polymorphic classes,

class Shape
{
public : virtual void draw()
{
}
virtual void sample();
};

class Circle : public Shape
{
public : virtual void draw()
{
cout<<"Circle::draw"<<endl;
}
};

int main()
{
Shape *ptr = new Circle;
ptr->draw();
return(0);
}

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?

Thanks in advance ! ! !
 
I

Ian Collins

Rahul said:
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.
 
M

Michael DOUBEZ

Rahul a écrit :
Hi Everyone,

I have the following polymorphic classes,

class Shape
{
public : virtual void draw()
{
}
virtual void sample();
};

class Circle : public Shape
{
public : virtual void draw()
{
cout<<"Circle::draw"<<endl;
}
};

int main()
{
Shape *ptr = new Circle;
ptr->draw();
return(0);
}

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?

If you make it virtual, 10.3-8 of the standard requires that:
"A virtual function declared in a class shall be defined or declared
pure or both; but no diagnostic is required."

Here you don't have the diagnostic of missing definition but the linker
requires it (for its virtual table).

But the standard doesn't require the definition of a function that is
not used (non-virtual sample() in your case). See 3.2 of the standard
for the ins and outs of when a function is *used*.

Michael
 
J

James Kanze

Rahul a écrit :
If you make it virtual, 10.3-8 of the standard requires that:
"A virtual function declared in a class shall be defined or
declared pure or both; but no diagnostic is required."

That's just a note; the normative text is in §3.2: "A virtual
function is used if it is not pure." If a function is used,
then there must be a definition (and only one, unless it is a
template or inline). Otherwise, undefined behavior occurs.

I wonder, however, if this shouldn't read: "a virtual function
is used if it is not pure, and the class is instantiated"? (I
can imagine scenarios where you pull in some header which
defines a class with virtual functions, but you don't actually
use the class, nor link against the library which contains the
definitions. As it now reads, this is undefined behavior.)
Here you don't have the diagnostic of missing definition but
the linker requires it (for its virtual table).

That's still a diagnostic. Not required, but in practice, I
think you'll almost always get it.
 
M

Michael DOUBEZ

Pete Becker a écrit :
Sure you do: the error message from the linker is a diagnostic. And
that's really the only point where this error could be detected.

Yes, only the linker could locate the missing symbol but I would prefer
a message like:
Error: undefined reference to 'void Shape::sample()'.

Well, still better than:
foo.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Which is even more cryptic.

Michael
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top