Linker errors on Template function definitions

P

PyongHopscotch

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,
const GAAlleleSet<T> & a,
GAGenome::Evaluator f=(GAGenome::Evaluator)0,
void * u=(void *)0) :
GA2DArrayAlleleGenome<T>(x, y, a, f, u)
{};

virtual ~Diploid2DArrayAlleleGenome() {};

static int FlipMutator(GAGenome & c, float pmut);
};

I'm using GAlib btw.

If I define the function FlipMutator here in the class declaration then
it works fine, but if I do this:

template <class T> int
Diploid2DArrayAlleleGenome<T>::FlipMutator(GAGenome & c, float pmut) {
... code
};

I get the unresolved external, but If I change <T> to the type of the
calling function (e.g. <int>) then it works fine, but of course isn't
polymorphic on type. Any ideas? Am I doing this right?

-Thanks
 
L

Luke Meyers

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:


I get the unresolved external, but If I change <T> to the type of the
calling function (e.g. <int>) then it works fine, but of course isn't
polymorphic on type. Any ideas? Am I doing this right?

This is in the FAQ:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.15
(and related sections)

Luke
 
P

PyongHopscotch

Ok I have a slightly different problem after implementing the C++ faq
solution. Everything seems to work fine if the functions are defined
not static, but when they are static I still get the linker error, even
after defining the functions for the specific instances used (e.g.
<int>)

But if I define in the .h file then there is no problem. Thoughts?

-Pyong
 
P

PyongHopscotch

NVM I figured it out, I was only making instances of the functions not
the whole class. Thanks again

-Pyong
 

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,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top