Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

STL: <list> and <map>

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
ChenChen
Asked:
ChenChen
  • 2
1 Solution
 
LoungeLizardCommented:
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
 
vbk_bgmCommented:
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
 
ChenChenAuthor Commented:
Thanks very much for your comment.
0
 
LoungeLizardCommented:
My pleasure!
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now