template specialization

Dmitry V. Kustov kustov@nusun.jinr.ru
Mon May 28 09:59:00 GMT 2001


I have a class:
--------------------------------------------------------------------------------------------------------

template <class InType, unsigned int ins>
class high_order_neuron
{
 public:
  high_order_neuron();
  double Output(const InType*);
  void RandomWeights(const double&,const unsigned int&);
  inline void ReturnWeights();
  void WeightUpdate(const double&,const double&);
  void WriteWeights(char*);
  void ReadWeights(char*);
  double weights[1<<ins];
 private:
  const InType *inputs;
  double weights0[1<<ins], dwei[1<<ins], *w;
  double summa, *Coeffinputs, *Coeffdwei;
  unsigned int i;
  template<int index> inline void OutputLoop(double);
  template<int step> inline void OutputCycle(double);
  void UpdateCycle(int,double);
};
/******************************************************************/
// Constructor
template <class InType, unsigned int ins>
high_order_neuron<InType,ins>::high_order_neuron()
{
// õÓÔÁÎÏ×ËÁ ÎÁÞÁÌØÎÙÈ ÚÎÁÞÅÎÉÊ ×ÅÓÏ× ÒÁ×ÎÙÍÉ ÎÕÌÀ.
  for (i=0;i<(1<<ins);i++) weights0[i] = weights[i] = dwei[i] = 0;
}
/*******************************************************************/
// Neuro output.
template <class InType, unsigned int ins>
double high_order_neuron<InType,ins>::Output(const InType *in)
{
  w = weights , summa = *w;
  inputs = in;
  OutputLoop<ins>(1.0);
  return summa;
}
/********************************************************************/
template <class InType, unsigned int ins>
template <int index>
inline void high_order_neuron<InType,ins>::OutputLoop(double prevVAL)
{
  OutputCycle<index-1>(prevVAL);
  summa += *(++w)**(inputs+index-1)*prevVAL;
}
/*********************************************************************/
template <class InType, unsigned int ins>
template <>
inline void high_order_neuron<InType,ins>::OutputLoop<0>(double prevVAL)
{};
/********************************************************************/
template <class InType, unsigned int ins>
template <int step>
inline void high_order_neuron<InType,ins>::OutputCycle(double prevVAL)
{
  double newVAL = *(inputs+step)*prevVAL;
  summa += *(++w)*newVAL;
  OutputLoop<step>(newVAL);
  OutputCycle<step-1>(prevVAL);
}
/*********************************************************************/
template <class InType, unsigned int ins>
template <>
inline void high_order_neuron<InType,ins>::OutputCycle<0>(double
prevVAL) {};
/*********************************************************************/
// éÚÍÅÎÅÎÉÅ ×ÅÓÏ×.
template <class InType, unsigned int ins>
void high_order_neuron<InType,ins>::WeightUpdate(const double &o,const
double &a)
{
  i = 0;
  weights0[0] = weights[0];
  weights[0] += dwei[0] = a*dwei[0];
  Coeffinputs = &o;
  Coeffdwei = &a;
  UpdateCycle(-1,1.0);
}
/*********************************************************************/
template <class InType, unsigned int ins>
void high_order_neuron<InType,ins>::UpdateCycle(int index,double
prevVAL)
{
 int k;

  for(k=index+1;k<ins-1;k++)
    {
      double newVAL = *(inputs+k)*prevVAL;
      weights0[i] = weights[++i];
      weights[i] += dwei[i] = *Coeffinputs*newVAL + *Coeffdwei*dwei[i];
      UpdateCycle(k,newVAL);
    }
  weights0[i] = weights[++i];
  weights[i] += dwei[i] = *Coeffinputs**(inputs+k)*prevVAL +
*Coeffdwei*dwei[i];
}
/**********************************************************************/

// ïÔÍÅÎÁ ÉÚÍÅÎÅÎÉÑ ×ÅÓÏ×.
template <class InType, unsigned int ins>
inline void high_order_neuron<InType,ins>::ReturnWeights()
{
  for (i=0;i<(1<<ins);i++) weights[i] = weights0[i];
}
/***********************************************************************/

// ðÒÉÂÁ×ÌÅÎÉÅ Ë ×ÅÓÁÍ ÓÌÕÞÁÊÎÙÈ ÚÎÁÞÅÎÉÊ × ÄÉÁÐÁÚÏÎÅ [-bound;bound].
template <class InType, unsigned int ins>
void high_order_neuron<InType,ins>::RandomWeights(const double &bound,
const unsigned int &choice)
{
  for (i=choice;i<(1<<ins);i++)
    {
      weights0[i] = weights[i];
#if OPERATING_SYSTEM == 0
#define RNDMAX 4294967295
      weights[i] += dwei[i] =
bound*tan(3.141592*arc4random()/RNDMAX-1.570796);
#else
      weights[i] += dwei[i] = bound*tan(3.141592*drand48()-1.570796);
#endif
    }
}
/************************************************************************/

// úÁÐÉÓØ ×ÅÓÏ× × ÏÔÄÅÌØÎÙÊ ÆÁÊÌ Ó ÉÍÅÎÅÍ FileName.
template <class InType, unsigned int ins>
void high_order_neuron<InType,ins>::WriteWeights(char *FileName)
{
  ofstream outClientFile(FileName,ios::out);
  if (! outClientFile)
    {
      cerr << "Something wrong with weight's file " << FileName << endl;

      return;
    }
  for (i=0;i<(1<<ins);i++) outClientFile << weights[i] << endl;
  outClientFile.close();
}
/***********************************************************************/

// þÔÅÎÉÅ ×ÅÓÏ× ÉÚ ÏÔÄÅÌØÎÏÇÏ ÆÁÊÌÁ Ó ÉÍÅÎÅÍ FileName.
template <class InType, unsigned int ins>
void high_order_neuron<InType,ins>::ReadWeights(char *FileName)
{
  ifstream inClientFile(FileName,ios::in);
  if (! inClientFile)
    {
      cerr << "Something wrong with weight's file " << FileName << endl;

      return;
    }
  for (i=0;i<(1<<ins);i++) inClientFile >> weights[i];
  inClientFile.close();
}
/***********************************************************************/

While compiling g++ 2.95.3 tells me:
honeuron.h:50: enclosing class templates are not explicitly specialized
honeuron.h:51: template-id `OutputLoop<0>' for
`high_order_neuron<InType,ins>::OutputLoop(double)' does not match any
template declaration
honeuron.h:51: syntax error before `{'
honeuron.h:64: enclosing class templates are not explicitly specialized
honeuron.h:65: template-id `OutputCycle<0>' for
`high_order_neuron<InType,ins>::OutputCycle(double)' does not match any
template declaration
honeuron.h:65: syntax error before `{'

How to specialize it correctly?

        With best regards,
                       Dmitry Kustov.



More information about the Gcc-help mailing list