| View previous topic :: View next topic |
| Author |
Message |
Dinakara Guest
|
Posted: Tue Jun 20, 2006 9:10 am Post subject: Compiling templates using GNU C++ |
|
|
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. |
|
| Back to top |
|
 |
Sharad Kala Guest
|
Posted: Tue Jun 20, 2006 9:10 am Post subject: Re: Compiling templates using GNU C++ |
|
|
"Gernot Frisch" <Me (AT) Privacy (DOT) 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 |
|
| Back to top |
|
 |
Gernot Frisch Guest
|
Posted: Tue Jun 20, 2006 9:10 am Post subject: Re: Compiling templates using GNU C++ |
|
|
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. |
|
| Back to top |
|
 |
Sharad Kala Guest
|
Posted: Tue Jun 20, 2006 9:10 am Post subject: Re: Compiling templates using GNU C++ |
|
|
"Dinakara" <Dinakara.gs (AT) in (DOT) 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-lite/templates.html#faq-35.15
Sharad |
|
| Back to top |
|
 |
|