By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Compiling templates using GNU C++

P: n/a
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...._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.
Jun 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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.
Jun 20 '06 #2

P: n/a

"Dinakara" <Di*********@in.bosch.com> wrote in message | 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.

Check this -- http://www.parashift.com/c++-faq-lit...html#faq-35.15

Sharad
Jun 20 '06 #3

P: n/a

"Gernot Frisch" <Me@Privacy.net> wrote in message |

| 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
Jun 20 '06 #4

P: n/a
Sharad Kala wrote :
The keyword is export and not extern. I don't think anyone other than Comeau
supports it.


ICC supports it.
Jun 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.