Solved

STL: <list> and <map>

Posted on 2002-05-26
4
471 Views
Last Modified: 2008-02-01
Would anyone know what the problem is with the following code? I am getting a compiling error C2535: 'const float *__thiscall std::allocator<float const >::address(const float &) const' : member function already defined or declared

/////////////////////////////////////////////////////
#include <list>
#include <map>
#include <string>

using namespace std;

typedef int                         Key;
typedef float                    VALUE;

typedef list<const VALUE>                    MYLIST;
typedef map<const Key,const VALUE*>          MYMAP;
typedef MYLIST::iterator                    POSL;
typedef MYMAP::iterator                         POSM;


class VCache{
     
public:
     VCache(int CacheSize){m_size=CacheSize;};

     inline void insert(const Key& k,const VALUE& v){
          if(m_list.size()<m_size){
               m_list.push_front(v);
               m_map.insert(std::make_pair(k,&v));
          }else{
               m_map.erase(m_getlistpos(&(m_list.back())));
               m_list.pop_back();
          }
     }

         

private:
     MYLIST               m_list;
     MYMAP               m_map;
     POSL               posl;
     POSM               posm;
     
     int                    m_size;

     inline POSM m_getlistpos(const VALUE* v){
          bool bfound=false;

          POSM pos;
          for(pos=m_map.begin();!bfound;pos++)
               bfound=(pos->second==vp);
         
          return pos;
     }

};

void main(void){
     VCache vc(10);
     Key k=1;
     VALUE v=0.25;
     vc.insert(k,v);
}
//End////////////////////////////////////////////////
0
Comment
Question by:ChenChen
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
LoungeLizard earned 50 total points
ID: 7036470
The problem is with the "const" qualifiers you use. The actual template specification for list<> looks like this:

template <class T, Class Allocator = allocator<T> > class list

You can see that we are actually specifying an “allocator” class also, which handles allocation of memory. Normally, we don’t need to do anything about this because it defaults to one of the “build-in” allocator classes that handles all the basic types for us (Thankfully, because writing your own allocator is not all that easy) However, this allocator class has got a function called address, which looks something like this:

T * address(T x) {return &x;}
const T * address(const T x) {return &x;}

Now, when you say list<const float>, this will translate to

const float * address(const float x) {return &x;}
const const float * address(const const float x) {return &x;}

Which the compiler has a hard time interpreting.

The good news is that you don’t need const. Just use normal “int” and “float” and then return list::const_iterator if you don’t want external classes messing up your values.
0
 
LVL 2

Expert Comment

by:vbk_bgm
ID: 7038851
You can define the collections without the const qualifier
typedef list<VALUE>                    MYLIST;
typedef map<Key,VALUE*>          MYMAP;
If you need const collections then,
const MYLIST list1;
const MYMAP map1;

For simple traversal you can use const iterator
MYLIST::const_iterator myit;
0
 
LVL 4

Author Comment

by:ChenChen
ID: 7041029
Thanks very much for your comment.
0
 
LVL 2

Expert Comment

by:LoungeLizard
ID: 7041067
My pleasure!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now