MAP Error

A

algatt

template<class K, class T, class W>
class AMI_cache_manager_static: public AMI_cache_manager_base {

private:
map<K,T> data_;
size_t current_size;
W writeout_;

public:
AMI_cache_manager_static(size_t capacity) :
AMI_cache_manager_base(capacity, 0), writeout_() {
current_size = 0;
}

~AMI_cache_manager_static(){
map<K,T>::iterator it;
it = data_.begin();
while (it != data_.end()) {
writeout_(it->second);
it++;
}
}

I have the above code, but where there is the map<K,T>::iterator it;
it gives me an error message that a ; must be placed before it.
If I change map<K,T>::iterator it; to map<string,string>::iterator it;
it works, but I need them as a template since they are not string.

Thanks for any help.
 
V

Victor Bazarov

algatt said:
template<class K, class T, class W>
class AMI_cache_manager_static: public AMI_cache_manager_base {

private:
map<K,T> data_;
size_t current_size;
W writeout_;

public:
AMI_cache_manager_static(size_t capacity) :
AMI_cache_manager_base(capacity, 0), writeout_() {
current_size = 0;
}

~AMI_cache_manager_static(){
map<K,T>::iterator it;

typename map said:
it = data_.begin();
while (it != data_.end()) {
writeout_(it->second);
it++;
}
}

I have the above code, but where there is the map<K,T>::iterator it;
it gives me an error message that a ; must be placed before it.
If I change map<K,T>::iterator it; to map<string,string>::iterator it;
it works, but I need them as a template since they are not string.

Read the FAQ, will you?

V
 
J

James Kanze

template<class K, class T, class W>
class AMI_cache_manager_static: public AMI_cache_manager_base {
private:
map<K,T> data_;
size_t current_size;
W writeout_;
public:
AMI_cache_manager_static(size_t capacity) :
AMI_cache_manager_base(capacity, 0), writeout_() {
current_size = 0;
}
~AMI_cache_manager_static(){
map<K,T>::iterator it;

You need to change this line to:
typename map said:
it = data_.begin();
while (it != data_.end()) {
writeout_(it->second);
it++;
}
}
I have the above code, but where there is the
map<K,T>::iterator it; it gives me an error message that a ;
must be placed before it.

The error message sounds a bit strange, but you definitly need
the typename.
If I change map<K,T>::iterator it; to map<string,string>::iterator it;
it works,

That's because the compiler knows that map<string,
string>::iterator is a type. For map<K,T>::iterator, it can't
know until it knows the types K and T. So you have to tell it.

The basic idea is that the compiler should be able to parse a
template definition when it sees it, in order to find obvious
errors. The problem is that the grammar of C++ isn't context
independent, and to part it, the compiler needs to know which
symbols name types. Since it can't possibly know in the above
case (there could be a specialization of map for the actual K
and T), you have to tell it. And if you lie, it's undefined
behavior. (Cruel world, isn't it.)
 
I

Ignaz Rutter

map<K,T>::iterator it;

*snip*
I have the above code, but where there is the map<K,T>::iterator it;
it gives me an error message that a ; must be placed before it.
If I change map<K,T>::iterator it; to map<string,string>::iterator it;
it works, but I need them as a template since they are not string.


You have to tell the Compiler that map<K,T>::iterator is indeed a type:

typename map<K,T>::iterator it;

From a purely syntactical viewpoint it might as well be a static
variable. And since the Compiler does not know what K and T are, it
cannot check what map<K,T>::iterator is. If you use map<string,
string>::iterator, the compiler can easily check that iterator is a type
and not a static variable (or function). That's why it works without
typename in that case.

Regards,
Ignaz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top