473,757 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linker error in virtual function

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 ! ! !
Jun 27 '08 #1
3 3598
On 2008-04-14 04:06:38 -0400, Michael DOUBEZ <mi************ @free.frsaid:
Rahul a écrit :
>>
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).
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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #2
On Apr 14, 10:06 am, Michael DOUBEZ <michael.dou... @free.frwrote:
Rahul a écrit :
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."
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.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3
On 2008-04-15 05:19:26 -0400, Michael DOUBEZ <mi************ @free.frsaid:
Pete Becker a écrit :
>On 2008-04-14 04:06:38 -0400, Michael DOUBEZ <mi************ @free.frsaid:
>>Rahul a écrit :

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

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() '.
That's a tricky one, though: it requires the linker to know how the
compiler mangles names, something that isn't otherwise necessary. On
many systems, the linker deals with multiple compilers for multiple
languages, and has developed over the course of decades, and is too
fragile to make any but the most essential changes.
>
Well, still better than:
foo.o(.eh_frame +0x11): undefined reference to `__gxx_personal ity_v0'
collect2: ld returned 1 exit status

Which is even more cryptic.
What's cryptic about that? <gIt means that, for whatever reason, you
didn't get the exception handling code. Probably by using "gcc" instead
of "g++" on the command line.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1962
by: Generic Usenet Account | last post by:
Don't you often wish that compiler and linker messages for templates were a little less cryptic and misleading? Consider this: I was getting this error message: undefined reference to `vtable for <class-name>' It turned out that I had not linked in the source for a virtual member function. In hindsight, the message does make sense, but I must admit that its verbiage threw me off for a while.
2
3389
by: Senapathy | last post by:
VC++ .NET 2003 Standard Edition Win XP, SP2 Hi, I have a code that builds under VC6.0, but fails to link in VC7.1. The offending code is as follows: _AFX_RICHEDIT_STATE* const pEditState = _afxRichEditState;
3
8081
by: Chucker | last post by:
Hi Folks, I got a Wrapper Dll around a native C++ static library. In .NET 1.1 this worked fine. When moving to .NET 2.0 I get a couple of unresolved externals / linker errors: Error 16 error LNK2028: unresolved token (0A000007) "extern "C" void __clrcall ___CxxCallUnwindDtor(void (__clrcall*)(void *),void *)" (?___CxxCallUnwindDtor@@$$J0YMXP6MXPAX@Z0@Z) referenced in function "public: virtual __thiscall...
4
1849
by: PyongHopscotch | last post by:
Hi All, So I'm getting the generic linker error (unresolved external error) in VC 7.0 when I define a templatized function. Here's some code examples: template <class T> class Diploid2DArrayAlleleGenome : public GA2DArrayAlleleGenome<T> { public: Diploid2DArrayAlleleGenome(unsigned int x, unsigned int y,
4
5880
by: Mark | last post by:
Hi, I'm trying to write some classes that kind of manage themselves. A linked list, that links different types of objects. Here's the code... object.h: --- class Object { int alt;
1
3293
by: developer | last post by:
Hi All I have made a .NET project. the files included are borland c++ files that i am migrate to VC++ .NET I am using Microsoft Visual C++ .NET 2003. the compilation goes through properly, but throws a load of linker errors
1
1765
by: toton | last post by:
In C++ , when the class is declared in the header & not implemented in the cpp, the linker is not giving a error untill the method gets called. How to ensure that all non pure-virtual method declared is implemented, even when the method/member function not getting called. Any linker checking can be performed for this purpose? How can a template class can be splitted in header & source? And to ensure every method is having an...
5
2667
by: Mark | last post by:
Sorry for creating such a newbish topic, but I just can't seem to figure out what the problem is here. // main.cpp #include <cstdlib> #include <iostream> #include "Vector.h" using namespace std;
3
2673
by: little.freaky | last post by:
Hello group, I have a problem with template classes and inheritance. I've searched on the internet to find a solution but all the examples look the same as my code (as far as I can tell) and I can't find my mistake (maybe there's something wrong with my eyes). I've two classes: template< class T >
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.