Temlate member of class, LNK2028 and Static Library.

P

pkoniusz

Hello everyone.

The problem may be obvious, though I'm a bit puzzled by the error
LNK2028 when attempting to utilize my static library. The all methods
of the class defined within that library do not pose any troubles, but
template based methods.

Let's say this is a header:
class LooseFunctions
{
private:
public:
// <== probe, if a given file can be opened for reading
bool CanOpenFileByName(const CHAR *psFileNameC);

template <class ElType>
ElType** AllocateBuffers(const unsigned nRequiredBuffSizeC,
const unsigned nRequiredElSizeC);
}

There is appropriate definition of body content in a cpp file. The
question is why the LNK2028 happend with resect to the above-declared
template method? When I copy and paste the body of that method to the
h file, all works fine. All remining methods (utilized in project)
that are not template based are perfectly happy to be infused with
their body in the cpp. WHat's the matter?

Cheers,
Peter.
 
B

Ben Voigt

Hello everyone.

The problem may be obvious, though I'm a bit puzzled by the error
LNK2028 when attempting to utilize my static library. The all methods
of the class defined within that library do not pose any troubles, but
template based methods.

There is appropriate definition of body content in a cpp file. The
question is why the LNK2028 happend with resect to the above-declared
template method? When I copy and paste the body of that method to the
h file, all works fine. All remining methods (utilized in project)
that are not template based are perfectly happy to be infused with
their body in the cpp. WHat's the matter?

Templates aren't compiled until the parameters are known, so the compiler
needs source code (not just a static library) to make new template
instances. So either put the template source in your header file, or
pre-instantiate all the different combinations you might want in the
library.
 
P

pkoniusz

The problem may be obvious, though I'm a bit puzzled by the error
Templates aren't compiled until the parameters are known, so the compiler
needs source code (not just a static library) to make new template
instances. So either put the template source in your header file, or
pre-instantiate all the different combinations you might want in the
library.

Hiah. I've assumed that this is the problem, though one more question.
How to preinstantiate a class along with these particular template
based methods? Would u give me example for the AllocateBuffers method
and, let's say, substitute the int type for ElType?

Cheers,
Peter.
 
B

Ben Voigt

Hiah. I've assumed that this is the problem, though one more question.
How to preinstantiate a class along with these particular template
based methods? Would u give me example for the AllocateBuffers method
and, let's say, substitute the int type for ElType?

http://www.comeaucomputing.com/techtalk/templates/#whylinkerror

I think it would be:

template <> int** AllocateBuffers<int>(const unsigned nRequiredBuffSizeC,
const unsigned nRequiredElSizeC);
 
T

Tom Widmer [VC++ MVP]

Tamas said:
Since it's a member function, you need to add "LooseFunctions::":

template int** LooseFunctions::AllocateBuffers<int>(const unsigned
nRequiredBuffSizeC, const unsigned nRequiredElSizeC);

Just to clarify, that should be in the CPP file along with the
non-template function definitions.

Tom
 

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

Top