Solved

STL: <list> and <map>

Posted on 2002-05-26
4
477 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

679 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