C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Compiling templates using GNU C++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Dinakara
Guest





PostPosted: Tue Jun 20, 2006 9:10 am    Post subject: Compiling templates using GNU C++ Reply with quote



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





PostPosted: Tue Jun 20, 2006 9:10 am    Post subject: Re: Compiling templates using GNU C++ Reply with quote



"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





PostPosted: Tue Jun 20, 2006 9:10 am    Post subject: Re: Compiling templates using GNU C++ Reply with quote



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





PostPosted: Tue Jun 20, 2006 9:10 am    Post subject: Re: Compiling templates using GNU C++ Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.