C++ template programming question expected `;' before ‘it’?
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
}
}
};
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++)
You got one or two typename declarations in the wrong place:
template<class DecayerPolicy>
class CDecayer: public DecayerPolicy, public CStateUpdater
{
public:
virtual void UpdateState(CAtom* patom)
{
typedef typename DecayerPolicy::indexpair indexpair;
typedef typename std::map<indexpair, double> mymap;
typedef typename mymap::const_iterator const_iterator;
//
for(const_iterator it = DecayerPolicy::mDecayRate.begin();
it!= DecayerPolicy::mDecayRate.end();
it++)
{
// atom state modification code
}
}
};
Though personally I would do this:
template<class DecayerPolicy>
class CDecayer: public DecayerPolicy, public CStateUpdater
{
typedef typename DecayerPolicy::indexpair indexpair;
typedef typename std::map<indexpair, double> mymap;
typedef typename mymap::const_iterator const_iterator;
typedef typename mymap::value_type value_type;
//
struct AtomStateModifier
{
void operator()(value_type const& data) const
{
}
};
//
public:
virtual void UpdateState(CAtom* patom)
{
std::for_each(DecayerPolicy::mDecayRate.begin(),
DecayerPolicy::mDecayRate.end(),
AtomStateModifier()
);
}
};
I've examined and modified your code (hopefully improving it lol). Works for me.
#include <iostream>
#include <map> // map contains <utility>
using namespace std;
struct CAtom
{
};
class CStateUpdater
{
public:
virtual void UpdateState(CAtom* patom) = 0;
}; // CStateUpdater
typedef std::pair<unsigned int, unsigned int> indexpair;
typedef std::map<indexpair, double> decay_map;
typedef decay_map::const_iterator decay_map_citerator;
class CDecayerPolicy
{
public :
CDecayerPolicy() { mDecayRate.clear(); }
~CDecayerPolicy() {}
const decay_map & getDecayRate() const { return mDecayRate; }
void setDecayRate(const decay_map & d_m) { mDecayRate = d_m; }
private :
decay_map mDecayRate;
};
template<class T> class CDecayer: public CStateUpdater
{
public:
CDecayer(const T & data) { policy = data; }
virtual void UpdateState(CAtom* patom)
{
for(decay_map_citerator it = policy.getDecayRate().begin(); it != policy.getDecayRate().end(); ++it)
{
// atom state modification code
cout << "[ "
<< it->first.first
<< " , "
<< it->first.second
<< " ] : "
<< it->second << endl;
}
}
private :
T policy;
};
int main()
{
const indexpair idx_p1 = indexpair(1, 5);
const indexpair idx_p2 = indexpair(2, 4);
const indexpair idx_p3 = indexpair(3, 3);
const indexpair idx_p4 = indexpair(4, 2);
const indexpair idx_p5 = indexpair(5, 1);
decay_map the_decay_map;
the_decay_map[idx_p1] = 3.0;
the_decay_map[idx_p2] = 5.2;
the_decay_map[idx_p3] = 6.4;
the_decay_map[idx_p4] = 1.4;
the_decay_map[idx_p5] = 6.5;
CDecayerPolicy the_policy;
the_policy.setDecayRate(the_decay_map);
CDecayer<CDecayerPolicy> the_decayer(the_policy);
the_decayer.UpdateState(NULL);
return 0;
}
Try #include <iterator>
The below appears to work. indexpair map isnt actually using any template types..?
typedef std::map<std::pair<unsigned int, unsigned int>, double> indexpairmap;
struct CDecayerPolicy
{
indexpairmap mDecayRate;
CDecayerPolicy()
{
mDecayRate.clear();
}
~CDecayerPolicy()
{}
};
template<class DecayerPolicy>
class CDecayer: public DecayerPolicy, public CStateUpdater
{
public:
virtual void UpdateState(CAtom* patom)
{
indexpairmap::iterator it = DecayerPolicy::mDecayRate.begin();
for(; it!= DecayerPolicy::mDecayRate.end(); it++)
{
// atom state modification code
}
}
};
精彩评论