Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > MAP Error

Reply
Thread Tools

MAP Error

 
 
algatt
Guest
Posts: n/a
 
      07-19-2007
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-19-2007
algatt wrote:
> 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<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.


Read the FAQ, will you?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      07-20-2007
On Jul 19, 10:06 pm, algatt <alang...@gmail.com> wrote:
> 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<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.


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.)

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Ignaz Rutter
Guest
Posts: n/a
 
      07-30-2007
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::map::find() throws exception when map is empty? Matthias Hildebrand C++ 5 03-20-2012 06:09 AM
I can map all files (.*) to asp.net worker.How do I map NO FILE to asp.net worker? alex ASP .Net 1 02-04-2005 03:18 AM
searching keys in std::map using map::upper_bound Erik Arner C++ 0 11-02-2004 11:14 PM
map.insert(key,val) vs. map[key]=val ? Patrick Guio C++ 6 10-20-2004 01:54 PM
map that maps to iterators in the same map ? Vlad C++ 0 12-15-2003 08:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57