Compiling templates using GNU C++

D

Dinakara

Hi,

I am programming with C++ using the gcc compiler.

Compiler : gcc version 3.4.3
OS : Red Hat 3.4.3-9.EL4.

I am not able to run the following program that declares a Template: Can
some body please help? There a total of three simple files whose contents,
which I have copied here.

//File 1 - MyTemplate.h

template < class T>
class MyTemplate
{
public:
MyTemplate();
};

//File 2 - MyTemplate.cpp

#include"MyTemplate.h"
template < class T>
MyTemplate<T>::MyTemplate()
{
cout<<"template Created"<<endl;
}

//File 3 - main.cpp

#include"MyTemplate.h"
#include <iostream>
using namespace std;
int main(void)
{
MyTemplate<int> oMytemplate;
}

1. This is how I am comiling the program : g++ main.cpp -o TemplateOut
This is the error I am getting : In function `main'::
undefined reference to `MyTemplate<int>::MyTemplate()', collect2: ld
returned 1 exit status

after taking a look at the gnu online docs,
http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_5.html#SEC110, I tried
compiling the code with g++ main.cpp -frepo -c -O. This created a Main.rpo
file that I do not know how to use.

All I want to see from this program is the line "template Created" to be
printed on the screen.

Thanks in advance.

- Dinakara.
 
G

Gernot Frisch

If you don't put the definition of a template function in the header,
you must use the keyword "extern". There's only very few compilers
that support it. GCC is not one.
Solution: inline your c'tor.
 
S

Sharad Kala

| If you don't put the definition of a template function in the header,
| you must use the keyword "extern". There's only very few compilers
| that support it. GCC is not one.

The keyword is export and not extern. I don't think anyone other than Comeau
supports it.

Sharad
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top