?
Solved

STL: <list> and <map>

Posted on 2002-05-26
4
Medium Priority
?
481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
LoungeLizard earned 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.
Suggested Courses

771 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