From: Jun on
Hi everyone,

Just a stupid problem, since i need to separate codes in header and
source files (for cross reference),
I found it's really difficult to work with templates in separated
source file.

For example:

// MyClass.h

template <class T>
struct MyClass{

Template <typename InputIterator>
T getMyBaby(InputIterator first, InputIterator last){}

};

How can I implement getMyBaby in separated cpp file?

since,

template<typename InputIterator> T template <class T>
MyClass<T>::getMyBaby( .... )

just doesn't work ...


Thank you very much


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
Jun wrote:
> I found it's really difficult to work with templates in separated
> source file.

Explained in the FAQ.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Bart van Ingen Schenau on
On Jun 4, 7:41 am, Jun <junh...(a)gmail.com> wrote:
> Hi everyone,
>
> Just a stupid problem, since i need to separate codes in header and
> source files (for cross reference),
> I found it's really difficult to work with templates in separated
> source file.

That is a very common problem.
There are basically three options:
1. Use the 'export' feature, which was designed for this.
Unfortunately, this feature is not very widely implemented.
2. Don't separate the functions into a separate source file.
3. #include the source file at the bottom of the header file.

Bart v Ingen Schenau


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: BanMido on
On Jun 4, 10:41 am, Jun <junh...(a)gmail.com> wrote:
> Hi everyone,
>
> Just a stupid problem, since i need to separate codes in header and
> source files (for cross reference),
> I found it's really difficult to work with templates in separated
> source file.
>
> For example:
>
> // MyClass.h
>
> template <class T>
> struct MyClass{
>
> Template <typename InputIterator>
> T getMyBaby(InputIterator first, InputIterator last){}
>
> };
>
> How can I implement getMyBaby in separated cpp file?
>
> since,
>
> template<typename InputIterator> T template <class T>
> MyClass<T>::getMyBaby( .... )
>
> just doesn't work ...
>

Take a look at the following link and see if it answers your question?
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Louis Lavery on
On 04/06/2010 06:41, Jun wrote:
> Hi everyone,
>
> Just a stupid problem, since i need to separate codes in header and
> source files (for cross reference),
> I found it's really difficult to work with templates in separated
> source file.
>
> For example:
>
> // MyClass.h
>
> template<class T>
> struct MyClass{
>
> Template<typename InputIterator>
> T getMyBaby(InputIterator first, InputIterator last){}
>
> };
>
> How can I implement getMyBaby in separated cpp file?
>
> since,
>
> template<typename InputIterator> T template<class T>
> MyClass<T>::getMyBaby( .... )
>
> just doesn't work ...
>

// MyClass.h
#ifndef MyClass_h
#define MyClass_h

// Declaration goes in here, just like non-template code

template<class T>
> struct MyClass{

Template<typename InputIterator>
T getMyBaby(InputIterator first, InputIterator last){}
};

#include "MyClass.cpp"
#endif


// MyClass.cpp
#ifndef MyClass_cpp
#define MyClass_cpp
#include "MyClass.h"

// Definition goes in here, just like non-template code

template<typename InputIterator> T template<class T>
MyClass<T>::getMyBaby( .... )

#endif


Louis.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]