| | |
template declaration
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 14
Reputation:
Solved Threads: 0
I have a header file : abc.h and abc.template file and i want to use the template feature, how can i modfiy following program to change it completely in template
abc.h file have
AND the abc.template file have
my question is how to change the srch and stat argument to the template type
i have tried using
template<class d, class c, class l>
void p<d>::search_s(c srch, const l& stat)
srch is the std:: string input from the keyboard
but the g++ compiler generate the error
and how to chagne it to template type in both the dot.h and dot.template file
abc.h file have
C++ Syntax (Toggle Plain Text)
#idndef d_h #define d_h # include header files - cstdlib, iostream and string namespace xyz { template<class d> class p; template<class d> std::ostream& operator <<(std::ostream& ou, const p<d>& a); template<class d> class p { public: p() { } //constructor ~p(); //destructor void abc(); void pattern(std::string, const bool& t); friend std::ostream& operator << <d> (std::ostream& ou, const p<d>& a); }; } #include"abc.template" #endif
AND the abc.template file have
C++ Syntax (Toggle Plain Text)
#include <cstdlib> other header too namespace CSCI301_double { template <class d> Double< d>::~Double() { } template <class d> void p<d>::search_s(string srch, const bool& stat) { // use so the argument srch and stat over here,, // how to use it } }
i have tried using
template<class d, class c, class l>
void p<d>::search_s(c srch, const l& stat)
srch is the std:: string input from the keyboard
but the g++ compiler generate the error
and how to chagne it to template type in both the dot.h and dot.template file
Last edited by Ancient Dragon; Oct 2nd, 2007 at 6:26 pm. Reason: add code tags
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
C++ Syntax (Toggle Plain Text)
template< typename T > class A { // ... template< typename SEARCH, typename STAT > inline void search_s( SEARCH srch, STAT stat ) { // implement here (inline) // some compilers complain otherwise. // can use the argument srch and stat here // ... } // ... };
Last edited by vijayan121; Oct 3rd, 2007 at 1:04 am.
•
•
Join Date: Sep 2007
Posts: 14
Reputation:
Solved Threads: 0
i got the information ,but how to implement it if the header file contains the function prototype with the template decalration and the implementaion file(other file) contains the namespace and template and function body defined on it and class implementaion in third file , i have already posted the sample of the code above
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
according to the c++ standard, this is how:
header file (abc.h):
the implementation file (abc.cc):
however, most current c++ compilers do not support export of templates ( comeau c++ (EDG) is the only one that i know of that does support this http://www.comeaucomputing.com/4.0/d...an/export.html ). for example this is the result of trying to compile this with gcc 4.2.2 (release candidate)
>g++42 -Wall -std=c++98 main.cc abc.cc
In file included from main.cc:1:
abc.h:5: warning: keyword 'export' not implemented, and will be ignored
In file included from abc.cc:1:
abc.h:5: warning: keyword 'export' not implemented, and will be ignored
abc.cc:5: warning: keyword 'export' not implemented, and will be ignored
abc.cc:10: warning: keyword 'export' not implemented, and will be ignored
abc.cc:14: warning: keyword 'export' not implemented, and will be ignored
/var/tmp//ccKsd40b.o(.text+0x24): In function `main':
: undefined reference to `my::abc<int>::abc(int const&)'
/var/tmp//ccKsd40b.o(.text+0x33): In function `main':
: undefined reference to `my::abc<int>::value() const'
/var/tmp//ccKsd40b.o(.text+0x58): In function `main':
: undefined reference to `void my::abc<int>::foo<char [6]>(char const (&) [6])'
collect2: ld returned 1 exit status
you could seperate the declaration and implementation into two seperate files (as long as you include one in another).
header file (abc_no_export.h):
definitions (abc.inl):
this has the same effect as having a single header file; i have seen people use this organization, but am not convinced about it's usefulness.
some compilers may baulk at even this; in which case you may have to define the member template function at the point of declaration:
header file (abc.h):
C++ Syntax (Toggle Plain Text)
#ifndef _ABC_H #define _ABC_H namespace my { export template< typename T > struct abc { explicit abc( const T& val = T() ) ; const T& value() const ; template< typename other > void foo( const other& x ) ; private: T _value ; }; } #endif // _ABC_H
C++ Syntax (Toggle Plain Text)
#include "abc.h" namespace my { export template< typename T > abc<T>::abc( const T& val ) : _value(val) { /* .... */ } export template< typename T > const T& abc<T>::value() const { return _value ; } export template< typename T > template< typename other > void abc<T>::foo( const other& x ) { /* .... */ } }
>g++42 -Wall -std=c++98 main.cc abc.cc
In file included from main.cc:1:
abc.h:5: warning: keyword 'export' not implemented, and will be ignored
In file included from abc.cc:1:
abc.h:5: warning: keyword 'export' not implemented, and will be ignored
abc.cc:5: warning: keyword 'export' not implemented, and will be ignored
abc.cc:10: warning: keyword 'export' not implemented, and will be ignored
abc.cc:14: warning: keyword 'export' not implemented, and will be ignored
/var/tmp//ccKsd40b.o(.text+0x24): In function `main':
: undefined reference to `my::abc<int>::abc(int const&)'
/var/tmp//ccKsd40b.o(.text+0x33): In function `main':
: undefined reference to `my::abc<int>::value() const'
/var/tmp//ccKsd40b.o(.text+0x58): In function `main':
: undefined reference to `void my::abc<int>::foo<char [6]>(char const (&) [6])'
collect2: ld returned 1 exit status
you could seperate the declaration and implementation into two seperate files (as long as you include one in another).
header file (abc_no_export.h):
C++ Syntax (Toggle Plain Text)
#ifndef _ABC_NO_EXPORT_H #define _ABC_NO_EXPORT_H namespace my { template< typename T > struct abc { explicit abc( const T& val = T() ) ; const T& value() const ; template< typename other > void foo( const other& x ) ; private: T _value ; }; } #include "abc.inl" // definitions #endif // _ABC_NO_EXPORT_H
C++ Syntax (Toggle Plain Text)
namespace my { template< typename T > abc<T>::abc( const T& val ) : _value(val) { /* .... */ } template< typename T > const T& abc<T>::value() const { return _value ; } template< typename T > template< typename other > void abc<T>::foo( const other& x ) { /* .... */ } }
some compilers may baulk at even this; in which case you may have to define the member template function at the point of declaration:
C++ Syntax (Toggle Plain Text)
#ifndef _ABC_NO_OUT_OF_LINE_MEMBER_TEMPLATE_FUNCTIONS_H #define _ABC_NO_OUT_OF_LINE_MEMBER_TEMPLATE_FUNCTIONS_H namespace my { template< typename T > struct abc { explicit abc( const T& val = T() ) ; const T& value() const ; template< typename other > void foo( const other& x ) { /* .... */ } private: T _value ; }; } #include "abc.inl" // other member function definitions #endif // _ABC_NO_OUT_OF_LINE_MEMBER_TEMPLATE_FUNCTIONS_H
![]() |
Similar Threads
- help (overload << in a template class ) (C++)
- Need help writing my own reverse iterator class (C++)
- Settle a discussion about Template contruct (C++)
- Linking separate files (C++)
- template link errors in visual studio .net 2003 (C)
- template issues - need expert debugger! (C++)
- How to code a friend template function? (C++)
Other Threads in the C++ Forum
- Previous Thread: Program For Image Access Using Borland C?
- Next Thread: meeting room capacity
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






