LNK2019 - Unresolved external symbol

R

repairman2003

Here's where the linker is running into an issue:

template <typename T>
void Engine::setEventReceiver(T receiver)
{
m_device->setEventReceiver(receiver);
}

I've got that function in a static library that my client program is
calling. I'm almost certain that this linker error is being caused
because the lib has no information about the type of object I'm
sending it. The type I'm passing into this is a custom type that
should not be in the lib file--it's a class that is holds client side
events and doesn't belong in this library.

here is the class info I'm using for the type:

class EventReceiver : public IEventReceiver
{
...
};

and the calling info in main:

Engine* engine...
EventReceiver receiver;

engine->setEventReceiver<EventReceive>(receiver);

Am I correct in why it is throwing that linker issue? What are my
options in getting around this?
 
R

repairman2003

No, you didn't. You may have some instantiations of that template
function, but you certainly don't have the template itself in a
static library.


You appear to have fundamental misunderstanding of how templates work.




The *body* of setEventReceiver() must be available here (e.g. from
an included header file). If (as I suspect) it isn't, then linker
error is entirely expected.


Put the body of template function into a header file, so it can be
instantiated when the type if 'T' is known.

Cheers,

Thanks for you quick response. It links correctly now. I had the
body of the template function in a .cpp file instead of in its .h
file. So you cannot have a template function definition in a header
file and its implementation in a separate file?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top