template declaration

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 14
Reputation: hectic is an unknown quantity at this point 
Solved Threads: 0
hectic hectic is offline Offline
Newbie Poster

template declaration

 
0
  #1
Oct 2nd, 2007
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
  1. #idndef d_h
  2. #define d_h
  3. # include header files - cstdlib, iostream and string
  4.  
  5. namespace xyz
  6. {
  7. template<class d>
  8. class p;
  9.  
  10. template<class d>
  11. std::ostream& operator <<(std::ostream& ou, const p<d>& a);
  12.  
  13. template<class d>
  14. class p
  15. {
  16. public:
  17. p() { } //constructor
  18. ~p(); //destructor
  19.  
  20. void abc();
  21. void pattern(std::string, const bool& t);
  22.  
  23. friend std::ostream& operator << <d> (std::ostream& ou, const p<d>& a);
  24. };
  25. }
  26. #include"abc.template"
  27. #endif

AND the abc.template file have

  1. #include <cstdlib> other header too
  2.  
  3.  
  4. namespace CSCI301_double
  5. {
  6.  
  7. template <class d>
  8. Double< d>::~Double()
  9. {
  10. }
  11. template <class d>
  12. void p<d>::search_s(string srch, const bool& stat)
  13. {
  14. // use so the argument srch and stat over here,,
  15. // how to use it
  16. }
  17. }
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
Last edited by Ancient Dragon; Oct 2nd, 2007 at 6:26 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: template declaration

 
1
  #2
Oct 3rd, 2007
  1. template< typename T >
  2. class A
  3. {
  4. // ...
  5. template< typename SEARCH, typename STAT >
  6. inline void search_s( SEARCH srch, STAT stat )
  7. {
  8. // implement here (inline)
  9. // some compilers complain otherwise.
  10. // can use the argument srch and stat here
  11. // ...
  12. }
  13. // ...
  14. };
Last edited by vijayan121; Oct 3rd, 2007 at 1:04 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 14
Reputation: hectic is an unknown quantity at this point 
Solved Threads: 0
hectic hectic is offline Offline
Newbie Poster

Re: template declaration

 
0
  #3
Oct 3rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: template declaration

 
0
  #4
Oct 3rd, 2007
according to the c++ standard, this is how:
header file (abc.h):
  1. #ifndef _ABC_H
  2. #define _ABC_H
  3. namespace my
  4. {
  5. export template< typename T > struct abc
  6. {
  7. explicit abc( const T& val = T() ) ;
  8. const T& value() const ;
  9. template< typename other > void foo( const other& x ) ;
  10. private: T _value ;
  11. };
  12. }
  13. #endif // _ABC_H
the implementation file (abc.cc):
  1. #include "abc.h"
  2.  
  3. namespace my
  4. {
  5. export template< typename T > abc<T>::abc( const T& val ) : _value(val)
  6. {
  7. /* .... */
  8. }
  9.  
  10. export template< typename T > const T& abc<T>::value() const
  11. {
  12. return _value ;
  13. }
  14. export template< typename T >
  15. template< typename other > void abc<T>::foo( const other& x )
  16. {
  17. /* .... */
  18. }
  19. }
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):
  1. #ifndef _ABC_NO_EXPORT_H
  2. #define _ABC_NO_EXPORT_H
  3. namespace my
  4. {
  5. template< typename T > struct abc
  6. {
  7. explicit abc( const T& val = T() ) ;
  8. const T& value() const ;
  9. template< typename other > void foo( const other& x ) ;
  10. private: T _value ;
  11. };
  12. }
  13. #include "abc.inl" // definitions
  14. #endif // _ABC_NO_EXPORT_H
definitions (abc.inl):
  1. namespace my
  2. {
  3. template< typename T > abc<T>::abc( const T& val ) : _value(val)
  4. {
  5. /* .... */
  6. }
  7.  
  8. template< typename T > const T& abc<T>::value() const
  9. {
  10. return _value ;
  11. }
  12. template< typename T >
  13. template< typename other > void abc<T>::foo( const other& x )
  14. {
  15. /* .... */
  16. }
  17. }
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:
  1. #ifndef _ABC_NO_OUT_OF_LINE_MEMBER_TEMPLATE_FUNCTIONS_H
  2. #define _ABC_NO_OUT_OF_LINE_MEMBER_TEMPLATE_FUNCTIONS_H
  3. namespace my
  4. {
  5. template< typename T > struct abc
  6. {
  7. explicit abc( const T& val = T() ) ;
  8. const T& value() const ;
  9. template< typename other > void foo( const other& x )
  10. {
  11. /* .... */
  12. }
  13.  
  14. private: T _value ;
  15. };
  16. }
  17. #include "abc.inl" // other member function definitions
  18. #endif // _ABC_NO_OUT_OF_LINE_MEMBER_TEMPLATE_FUNCTIONS_H
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC