It's late and I can't figu开发者_如何学Cre out what is wrong with my syntax. I have asked other people and they can't find the syntax error either so I came here on a friend's advice.
template <typename TT>
bool PuzzleSolver<TT>::solve ( const Clock &pz ) {
possibConfigs_.push( pz.getInitial() );
vector< Configuration<TT> > next_;
//error is on next line
map< Configuration<TT> ,Configuration<TT> >::iterator found;
while ( !possibConfigs_.empty() && possibConfigs_.front() != pz.getGoal() ) {
Configuration<TT> cfg = possibConfigs_.front();
possibConfigs_.pop();
next_ = pz.getNext( cfg );
for ( int i = 0; i < next_.size(); i++ ) {
found = seenConfigs_.find( next_[i] );
if ( found != seenConfigs_.end() ) {
possibConfigs_.push( next_[i] );
seenConfigs_.insert( make_pair( next_[i], cfg ) );
}
}
}
}
What is wrong?
Thanks for any help.
If I remember correctly, this syntax is ambiguous:
map< Configuration<TT> ,Configuration<TT> >::iterator found;
Try that instead:
typename map< Configuration<TT> ,Configuration<TT> >::iterator found;
For dependent names you need to use the typename keyword. A dependent name is a name that depends on a template parameter.
So you have to:
typename map< Configuration<TT> ,Configuration<TT> >::iterator found;
This is necessary because the type of your map isn't known until your class (PuzzleSolver) is instantiated, since it depends on the template parameter TT.
This is a classic example of the required use of typename
to identify named types that depend on the template type parameter. You want to use
typename map< Configuration<TT>, Configuration<TT> >::iterator found;
Basically, the compiler can't know that map< Configuration<TT>, Configuration<TT> >::iterator
is a type rather than, e.g. a member variable, unless you tell it. Anytime you are using a named type that depends on the template parameter(s) you must use typename
(except in a few exceptional cases such as in the initialization list of a constructor).
精彩评论