Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template programming question expected `;' before ‘it’?

Tags:

c++

templates

I am writting a small piece of code exercising policy-based template programming. In this program, a CDecayer class is defined and it uses DecayerPolicy as its policy class. However the compiler complained that " expected `;' before ‘it’ " about the CDecayer part . Any suggestions?

#include <iostream>
#include <vector>
#include <map>
#include <utility>



int main()
{
}

struct CAtom
{
};

class CStateUpdater
{
public:
  virtual void UpdateState(CAtom* patom) = 0;
};


struct CDecayerPolicy
{
  typedef std::pair<unsigned int, unsigned int> indexpair;
  std::map<indexpair, double> mDecayRate;

  CDecayerPolicy()
  { 
    mDecayRate.clear();
  }

  ~CDecayerPolicy()
  {}
};



template<class DecayerPolicy>
class CDecayer: public DecayerPolicy, public CStateUpdater
{
public:
  virtual void UpdateState(CAtom* patom)
  {
    for(std::map<DecayerPolicy::indexpair, double >::const_iterator it =  DecayerPolicy::mDecayRate.begin(); it!= DecayerPolicy::mDecayRate.end(); it++)
      {
// atom state modification code
      }
  }
};
like image 952
Kuang Chen Avatar asked Dec 02 '22 06:12

Kuang Chen


1 Answers

You need to add typename before dependent types, i.e.

for(typename std::map<typename DecayerPolicy::indexpair, double >::const_iterator
    it = DecayerPolicy::mDecayRate.begin();
    it != DecayerPolicy::mDecayRate.end();
    it++)
like image 73
avakar Avatar answered Dec 04 '22 14:12

avakar