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

#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

#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
  }
}

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

Recommended Answers

All 3 Replies

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
        // ...
      }
  // ...
};
commented: I have been lax on giving you the reputation points have deserve. +11

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

according to the c++ standard, this is how:
header file (abc.h):

#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

the implementation file (abc.cc):

#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 )
    {
       /* .... */
    }
}

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/docs/userman/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):

#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

definitions (abc.inl):

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 )
    {
       /* .... */
    }
}

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:

#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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.