Board index » cppbuilder » 'map' container (STL) (BCB3 and BCB5)

'map' container (STL) (BCB3 and BCB5)

In my project (version C++ Builder 3) I used map container.
After compiling this project in the C++ Builder 5 version I had some errors.
One of them E2024 Cannot modify a const object        Compiler error

My Solution:
1.
I opened the map.h file
In the line 86 and 102 we can see code like this:

#ifndef _RWSTD_NO_CONST_INST
  class Allocator = allocator<pair<const Key,T> > >
#else
  class Allocator = allocator<pair<Key,T> > >

2.
I checked #include <stdcomp.h>  file (include of the map.h)
and find the constant  _RWSTD_NO_CONST_INST
This constant is not defined after line 993
(//   Borland C++ Builder 5.0 (BCB)) in the stdcomp.h file
I compared stdcomp.h file of the version C++ Builder 3 and version C++ Builder 5.

And my solution is:
# define _RWSTD_NO_CONST_INST                   1

I put this definition in the 1005 line in the stdcomp.h
After that I did not get this error, but this solution is not so good.

Could you please find a more adequate solution  

Thanks. Luba

 

Re:'map' container (STL) (BCB3 and BCB5)


Quote
"luba roditi" <l...@dsi.co.il> writes:
> In my project (version C++ Builder 3) I used map container.
> After compiling this project in the C++ Builder 5 version I had some errors.
> One of them  E2024 Cannot modify a const object  Compiler error

What are you doing with your map to cause this problem?  A piece of
source code would be helpful to anyone trying to help you...

--
Chris (TeamB);

Re:'map' container (STL) (BCB3 and BCB5)


"Chris Uzdavinis (TeamB)" <ch...@atdesk.com> wrote:

Quote
>"luba roditi" <l...@dsi.co.il> writes:

>> In my project (version C++ Builder 3) I used map container.
>> After compiling this project in the C++ Builder 5 version I had some errors.
>> One of them  E2024 Cannot modify a const object  Compiler error

>What are you doing with your map to cause this problem?  A piece of
>source code would be helpful to anyone trying to help you...

>--

for example: I need get all information from container after
             mouse move action.

container definition:
// container map   (key(int) +
//                  data(attached draw data = Attached_Typ)
typedef map<int,Attached_Typ,less<int> > AttachedTargMap;
typedef AttachedTargMap::value_type entry_AttachedTargMap;

///////////////////////////////////////////////////////////

My function is:
int __fastcall TGSSTarget::CursorMoving(int imageX,
                                        int imageY,
                                        entry_TargMap &aentry,
                                        double scale,
                                        TPoint center,
                                        int rel_angle)
{
  TPoint max, min;
  TPoint pixelXY;
  TargMap::iterator i = targetData.begin();
  for (;i != targetData.end();i++)
  {
   double x = (*i).second.target.x_pos;
   double y = (*i).second.target.y_pos;
   if (rel_angle != -1)
   {
    x = x*cos(-rel_angle*M_PI/180) + y*sin(-rel_angle*M_PI/180);
    y = -x*sin(-rel_angle*M_PI/180) + y*cos(-rel_angle*M_PI/180);
    pixelXY = TMoveModel::GetPixelCoordinate(x,y,center,scale);
    }
    else
    {
      // convert real coordinates to the pixel coordinates
      pixelXY = TMoveModel::GetPixelCoordinate(x,y,center,scale);
    }

    max.x = pixelXY.x + SPRITE_WIDTH/2;
    max.y = pixelXY.y + SPRITE_HEIGHT/2;
    min.x = pixelXY.x - SPRITE_WIDTH/2;
    min.y = pixelXY.y - SPRITE_HEIGHT/2;

    if (imageX < max.x && imageX > min.x &&
        imageY < max.y && imageY > min.y)
    {// THE PROBLEM WITH ASSIGNMENT TO THE key OF THE CONTAINER  
       aentry.first = (*i).first;   //////HERE I HAVE THIS ERROR
       aentry.second = (*i).second;
       return (*i).first;
    }
  }
  return -1;

Quote
}

Re:'map' container (STL) (BCB3 and BCB5)


Quote
"luba roditi" <l...@dsi.co.il> writes:
>     if (imageX < max.x && imageX > min.x &&
>         imageY < max.y && imageY > min.y)
>     {// THE PROBLEM WITH ASSIGNMENT TO THE key OF THE CONTAINER  
>        aentry.first = (*i).first;   //////HERE I HAVE THIS ERROR
>        aentry.second = (*i).second;
>        return (*i).first;
>     }
>   }

This isn't a const problem, nor a bug in the compiler.  You're just
trying to do something that is invalid.  You should thank your
compiler for catching that mis-use of the map.  Suppose that it
actually allowed you to change the key....

What would happen?

The map is implemented in terms of a red-black mostly-balanced
tree, sorted by key.  If the key were to change, you could (and most
likely would) have a node in the "wrong" place, so that the order of
the nodes is no longer sorted.  Once that happens, all searches
through the map are invalid, because you may not find items that are
really there, since it is using a search algorithm that depends on the
sorted order property that a RB-tree is supposed to guarantee.

What you need to do is two things.  First, think about why you need to
change the key.  If the key is going to change, you probably are
representing your data incorrectly.  This is very inefficient, and
probably can be represented better.  I'm not familiar with your
problem enough to know what to suggest, but something just isn't right.

Second, if you decide that "yes, I really want to change my map's key"
then you have to do more coding than just assigning a new key to the
iterator's first member.  You must insert a new entry into the map
with the new key and the old value, then erase the old key entry from
the map.  Yes, it's slow and a lot of work, but that's what you have
to do.  That's also why you should think about solving this in a
differnet way than a map.

PS, STL questions should be asked in the .language group, not the
IDE.  

--
Chris (TeamB);

Re:'map' container (STL) (BCB3 and BCB5)


"Chris Uzdavinis (TeamB)" <ch...@atdesk.com> wrote:

Quote
>"luba roditi" <l...@dsi.co.il> writes:

I don't want change my map's key.
I need just get all data (including key) to reperesent it
to the user. (when the mouse pointer moves over my control(TImage)) =>

Quote
>     if (imageX < max.x && imageX > min.x &&
>         imageY < max.y && imageY > min.y)
>        aentry.first = (*i).first;   //here key isn't changed

                                      //i just assign it to
                                      //the temporary object
Quote
>        aentry.second = (*i).second;
>        return (*i).first;
>     }

aentry - the temporary object

Thanks, Luba

Re:'map' container (STL) (BCB3 and BCB5)


Quote
"luba roditi" <l...@dsi.co.il> writes:
> "Chris Uzdavinis (TeamB)" <ch...@atdesk.com> wrote:
> >"luba roditi" <l...@dsi.co.il> writes:

> I don't want change my map's key.
> I need just get all data (including key) to reperesent it
> to the user. (when the mouse pointer moves over my control(TImage)) =>

> >     if (imageX < max.x && imageX > min.x &&
> >         imageY < max.y && imageY > min.y)
> >        aentry.first = (*i).first;   //here key isn't changed
>                                       //i just assign it to
>                                       //the temporary object

Well, you never posted what type entry_TargMap was, and I assumed that
you actually meant entry_AttachedTargMap, which is the only type that
is remotely close.  Being the value_type from a map, it's defined as
such:

  typedef std::pair<const Key, T> value_type;

The "first" part of a value_type is NOT changable even if it's not a
value type that is actually in a map.  If you're just wanting to
return data from the function, you can't use value_type, you should
create your own pair type that has a non-const first parameter.

--
Chris (TeamB);

Other Threads