C ++ template programming issue is expected `; ' before that?

I am writing a small piece of code that uses policy-based programming. This program defines the CDecayer class and uses DecayerPolicy as its policy class. However, the compiler complained that it was "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 } } }; 
+4
source share
5 answers

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

 for(typename std::map<typename DecayerPolicy::indexpair, double >::const_iterator it = DecayerPolicy::mDecayRate.begin(); it != DecayerPolicy::mDecayRate.end(); it++) 
+17
source

You have one or two type name 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 } } }; 

Although 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() ); } }; 
+1
source

I have studied and modified your code (I hope it improves it). It 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; } 
+1
source

Try #include <iterator>

0
source

The following shows how it works. The indexpair map does not actually use 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 } } }; 
0
source

All Articles