VStudio9 and template member functions

R

riccardo.gherardi

Hello.
The code below results in a linker error when compiled with Visual
Studio 2008, v9.0.21022.8 RTM.
The problem disappears when Foo::Bar implementation is moved to the
header file.
I've read the relevant template FAQs suggesting to never separate
definition and declaration of template member functions, but they deal
with code that fails to compile.
It's a sintax issue (and therefore my fault) or a compiler quirk?
Thanks in advance, R

// --- foo.h ---

class Foo
{
public:
template<class T> int Bar(int);
};

// --- foo.cpp ---

#include "foo.h"
template<class T> int Foo::Bar(int)
{
return 0;
}

// --- main.cpp ---

#include "foo.h"
int main()
{
Foo f;
int x = f.Bar<double>(3);
}
 
J

james.fysh

Thanks.
The referenced solution, telling the compiler what code to generate,
works.
In the aforementioned case, it means adding the following line to
foo.cpp.

    template int Foo::Bar<double>(int);

Now I just have to manually keep track of the instantiation I wish to
use... :b R

Or you could just define the functionality inside the header file, so
you don't have to explicitly declare each type you want to use.
 
A

anon

Or you could just define the functionality inside the header file, so
you don't have to explicitly declare each type you want to use.

Or include the cpp 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,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top