template class syntax

P

Patrick Guio

Hi,

I have trouble to compile the following piece of code with g++3.4 but not
with earlier version

// Foo.h
template<typename T>
class Foo
{
public:
void f();
};

// Foo.cpp
#include <iostream>
#include "Foo.h"

template<typename T>
void Foo<T>::f()
{
std::cout << "Foo<T>::f()\n";
}

template Foo<int>;


// main.cpp
#include "Foo.h"

int main()
{
Foo<int> x;
x.f();
}


The error message when using g++3.4 is

Foo.cpp:10: error: expected unqualified-id before ';' token

i.e. the template command "template Foo<int>;"

Is this syntax not standard?

Cheers,

Patrick
 
Z

zeotherm

Patrick said:
Hi,

I have trouble to compile the following piece of code with g++3.4 but not
with earlier version

// Foo.h
template<typename T>
class Foo
{
public:
void f();
};

// Foo.cpp
#include <iostream>
#include "Foo.h"

template<typename T>
void Foo<T>::f()
{
std::cout << "Foo<T>::f()\n";
}

template Foo<int>;


// main.cpp
#include "Foo.h"

int main()
{
Foo<int> x;
x.f();
}


The error message when using g++3.4 is

Foo.cpp:10: error: expected unqualified-id before ';' token

i.e. the template command "template Foo<int>;"

Is this syntax not standard?

Cheers,

Patrick


Patrick, I believe that you need to move the definition of you f()
function into the header file. Templates need to have their member
function defined in the header where the class is defined or you will
recieve a linker error (If I recall correctly). So move what is in
foo.cpp into foo.h. Also, I am not sure what you are trying to
accomplish with the line "template Foo<int>;"... Hope this will help
you out some.

- ZT
 
D

Dietmar Kuehl

zeotherm said:
Patrick, I believe that you need to move the definition of you f()
function into the header file. Templates need to have their member
function defined in the header where the class is defined or you will
recieve a linker error (If I recall correctly).

You recall only half correctly: with most compilers you need to put
template definitions into header files if you use implicit
instantiation. However, he actually tries to do explicit instantiation
which removes the need of having the template definition in the header.
So move what is in foo.cpp into foo.h.

A better advice is: use the correct syntax for explicit instantiation!
Also, I am not sure what you are trying to
accomplish with the line "template Foo<int>;"...

This look much like an explicit instantiation but is not quite. The
correct syntax is this:

template class Foo<int>;

This instantiates all member of the class template 'Foo' with the
template parameter 'int'. After this instantiation, you can use
'Foo<int>' in all translation units without putting the definitions
of the member functions into a header. Of course, if you need 'Foo'
for other types, too, you either need to add appropriate
instantiations or make the definitions available for implicit
instantiation.
 

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,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top