Undefined reference to vtable

K

Karl Ebener

Hi!

I have created a program using several classes with inheritage. When I
compile and link, I get the following error:

:$ g++ service2.cpp Service.cpp Application.cpp Message.cpp
MessageQueue.cpp MessageFragmenter.cpp SingleMessage.cpp
/tmp/ccsrT4jU.o(.gnu.linkonce.t._ZN9c_ServiceD1Ev+0xb): In function
`c_Service::~c_Service [in-charge]()':
: undefined reference to `vtable for c_Service'
/tmp/ccF4Q9O0.o(.text+0x27): In function
`c_Service::c_Service[not-in-charge](long, bool)':
: undefined reference to `vtable for c_Service'
/tmp/ccF4Q9O0.o(.text+0x8b): In function
`c_Service::c_Service[in-charge](long, bool)':
: undefined reference to `vtable for c_Service'
collect2: ld returned 1 exit status

I think, I had this error once before, but I don't remember, what I did
then. What is this vtable? It is not an attribute of my classes.

What's wrong??

Tnx very much!
Karl
 
V

Victor Bazarov

Karl said:
I have created a program using several classes with inheritage. When I
compile and link, I get the following error:

:$ g++ service2.cpp Service.cpp Application.cpp Message.cpp
MessageQueue.cpp MessageFragmenter.cpp SingleMessage.cpp
/tmp/ccsrT4jU.o(.gnu.linkonce.t._ZN9c_ServiceD1Ev+0xb): In function
`c_Service::~c_Service [in-charge]()':
: undefined reference to `vtable for c_Service'
/tmp/ccF4Q9O0.o(.text+0x27): In function
`c_Service::c_Service[not-in-charge](long, bool)':
: undefined reference to `vtable for c_Service'
/tmp/ccF4Q9O0.o(.text+0x8b): In function
`c_Service::c_Service[in-charge](long, bool)':
: undefined reference to `vtable for c_Service'
collect2: ld returned 1 exit status

I think, I had this error once before, but I don't remember, what I did
then. What is this vtable?

It's a way of implementing polymorphism, a table of pointers to virtual
member functions of the class.
It is not an attribute of my classes.

Not explicitly, anyway.
What's wrong??

Are you trying to call a virtual function from a constructor? Post your
code. A call to virtual function shouldn't go through vtable, though.
Do you have virtual functions? Just post your code.

V
 
K

Karl Ebener

Hi!

Tnx at first. It has something to do with a virtual function. When I
make it non-virtual, it works.

Code follows:

Service.h:
-------------
class c_Service : public c_Application
{
private:
string name;
long id;
long brokerId;
public:
c_Service(long serviceId, bool cachaeable = true);
int sendResult(long destinationId, string result);
void receiveRequest();
c_Message getResult(string request);
bool signUp(long broker);

void run();

/* this has to be implemented by a service
* Result is a c_Message containing the content, the service wants to send
*/
virtual c_Message calculateResult(string request);
};

#endif // SERVICE_H


Service.cpp:
-----------------
c_Service::c_Service(long serviceId, bool cachaeable) :
c_Application(serviceId)
{
}

int c_Service::sendResult(long clientId, string request)
{
}

c_Message c_Service::getResult(string request)
{
}

bool c_Service::signUp(long broker)
{
return c_Application::signUp(broker, SIGNUPSERVICE);
}

void c_Service::run()
{
}
 
V

Victor Bazarov

Karl said:
Tnx at first. It has something to do with a virtual function. When I
make it non-virtual, it works.

Does 'c_Application' class have any virtual functions? If you intend to
use 'c_Service' polymorphically, do you have 'c_Application's member
'calculateResult' declared "virtual" as well? Is its destructor virtual?
It may not have anything to do with your problem, but you better know the
answers...
Code follows:

Service.h:
-------------
class c_Service : public c_Application
{
private:
string name;
long id;
long brokerId;
public:
c_Service(long serviceId, bool cachaeable = true);
int sendResult(long destinationId, string result);
void receiveRequest();
c_Message getResult(string request);
bool signUp(long broker);

void run();

/* this has to be implemented by a service
* Result is a c_Message containing the content, the service wants
to send
*/
virtual c_Message calculateResult(string request);
};

#endif // SERVICE_H


Service.cpp:
-----------------
c_Service::c_Service(long serviceId, bool cachaeable) :
c_Application(serviceId)
{
}

int c_Service::sendResult(long clientId, string request)
{
}

c_Message c_Service::getResult(string request)
{
}

bool c_Service::signUp(long broker)
{
return c_Application::signUp(broker, SIGNUPSERVICE);
}

void c_Service::run()
{
}

I don't see 'calculateResult' here.

V
 
A

Andrew Koenig

I have created a program using several classes with inheritage. When I
compile and link, I get the following error:

:$ g++ service2.cpp Service.cpp Application.cpp Message.cpp
MessageQueue.cpp MessageFragmenter.cpp SingleMessage.cpp
/tmp/ccsrT4jU.o(.gnu.linkonce.t._ZN9c_ServiceD1Ev+0xb): In function
`c_Service::~c_Service [in-charge]()':
: undefined reference to `vtable for c_Service'

For many C++ implementations, this message is misleading. What's really
going on is that you did not define one of your virtual functions.

Specifically, for the implementations I've seen, it is the first virtual
function in your class that is not inline.

So, for example:

class c_Service {
public:
virtual void foo() { }
void foo2();
virtual void bar();
};

Because c_Service::foo is inline, you can ignore it for these purposes.
Because c_Service::foo is not virtual, you don't need to define it unless
you call it.
However, c_Service::bar is virtual and not inline, so it must be defined --
even if you never use it.

If you fail to define c_Service::bar, I would not be at all surprised if you
get the diagnostic message that you're seeing.
 
D

David Harmon

On Fri, 19 Nov 2004 17:14:29 +0100 in comp.lang.c++, Karl Ebener
: undefined reference to `vtable for c_Service'

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[23.7] What does it mean that the "virtual table" is an unresolved
external?"
It is always good to check the FAQ before posting. You can get the
FAQ at:
http://www.parashift.com/c++-faq-lite/
 
K

Karl Ebener

Hi!

Tnx to all of you. you helped me point out, that my error was not to
implement my virtual function.
I did so, because I thought I *must not* implement virtual functions.

Well, that was an error. Tnx very much!

Karl
 

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

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top