Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   behavior of a std::map I don't understand... (http://www.velocityreviews.com/forums/t552390-behavior-of-a-std-map-i-dont-understand.html)

Heck 11-15-2007 11:12 PM

behavior of a std::map I don't understand...
 
I'm using Visual Studio 2005, but i don't think that's (primarily)
where I've gone wrong.

I was experimenting, trying, by the way, to move into writing my own
iterator so that I can learn, ultimately, to write templates:
std::map< int, char [12]> cm;
char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};

cm[0] = str0;

The compiler says about that assignment,
left operand must be l-value

What does this mean, what's happened here?

It compiles without error when the value arg in the map is instead a
char or a char *.
std::map< int, char *> cm;

Thanks for your help and knowledge.

Harold

red floyd 11-15-2007 11:20 PM

Re: behavior of a std::map I don't understand...
 
Heck wrote:
> I'm using Visual Studio 2005, but i don't think that's (primarily)
> where I've gone wrong.
>
> I was experimenting, trying, by the way, to move into writing my own
> iterator so that I can learn, ultimately, to write templates:
> std::map< int, char [12]> cm;
> char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};
>
> cm[0] = str0;
>
> The compiler says about that assignment,
> left operand must be l-value
>
> What does this mean, what's happened here?
>
> It compiles without error when the value arg in the map is instead a
> char or a char *.
> std::map< int, char *> cm;
>


Arrays are not assignable.

=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= 11-16-2007 05:41 AM

Re: behavior of a std::map I don't understand...
 
On 2007-11-16 00:12, Heck wrote:
> I'm using Visual Studio 2005, but i don't think that's (primarily)
> where I've gone wrong.
>
> I was experimenting, trying, by the way, to move into writing my own
> iterator so that I can learn, ultimately, to write templates:
> std::map< int, char [12]> cm;
> char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};
>
> cm[0] = str0;
>
> The compiler says about that assignment,
> left operand must be l-value
>
> What does this mean, what's happened here?
>
> It compiles without error when the value arg in the map is instead a
> char or a char *.
> std::map< int, char *> cm;


You can not store an array in a map (or any other collection for that
matter).

--
Erik Wikström

Juha Nieminen 11-16-2007 10:14 AM

Re: behavior of a std::map I don't understand...
 
Heck wrote:
> char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};


Btw, you can save typing by simply writing:

char str0[12] = "first text";

LR 11-16-2007 03:58 PM

Re: behavior of a std::map I don't understand...
 
Erik Wikström wrote:

> You can not store an array in a map (or any other collection for that
> matter).


Sorry, I don't understand what you mean by collection. Could you please
expand on that?

TIA

LR


Victor Bazarov 11-16-2007 04:06 PM

Re: behavior of a std::map I don't understand...
 
LR wrote:
> Erik Wikstrm wrote:
>
>> You can not store an array in a map (or any other collection for that
>> matter).

>
> Sorry, I don't understand what you mean by collection. Could you
> please expand on that?


Erik will hopefully give his POV, but AFAICT, Erik meant any standard
container by "any other collection".

Arrays do not satisfy the requirements for the contained items. The
main two are Assignable and Copy-constructible. Arrays are neither.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



jeffrey.j.schmitz@gmail.com 11-16-2007 04:30 PM

Re: behavior of a std::map I don't understand...
 
On Nov 15, 11:41 pm, Erik Wikstrm <Erik-wikst...@telia.com> wrote:
> On 2007-11-16 00:12, Heck wrote:
>
>
>
> > I'm using Visual Studio 2005, but i don't think that's (primarily)
> > where I've gone wrong.

>
> > I was experimenting, trying, by the way, to move into writing my own
> > iterator so that I can learn, ultimately, to write templates:
> > std::map< int, char [12]> cm;
> > char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};

>
> > cm[0] = str0;

>
> > The compiler says about that assignment,
> > left operand must be l-value

>
> > What does this mean, what's happened here?

>
> > It compiles without error when the value arg in the map is instead a
> > char or a char *.
> > std::map< int, char *> cm;

>
> You can not store an array in a map (or any other collection for that
> matter).
>
> --
> Erik Wikstrm


Wrap your array in a class and define the assignment operator

Jeff

http://theschmitzer.blogspot.com

=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= 11-16-2007 04:35 PM

Re: behavior of a std::map I don't understand...
 
On 2007-11-16 16:58, LR wrote:
> Erik Wikström wrote:
>
>> You can not store an array in a map (or any other collection for that
>> matter).

>
> Sorry, I don't understand what you mean by collection. Could you please
> expand on that?


Sorry about that, I have been programming to much VBA (ugh!) lately.
Just like Victor guessed I meant a standard container.

--
Erik Wikström

Heck 11-18-2007 03:17 AM

Re: Re: behavior of a std::map I don't understand...
 
Erik Wikstrm!
>On 2007-11-16 00:12, Heck wrote:
>> I'm using Visual Studio 2005, but i don't think that's (primarily)
>> where I've gone wrong.
>>
>> I was experimenting, trying, by the way, to move into writing my own
>> iterator so that I can learn, ultimately, to write templates:
>> std::map< int, char [12]> cm;
>> char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};
>>
>> cm[0] = str0;
>>
>> The compiler says about that assignment,
>> left operand must be l-value
>>
>> What does this mean, what's happened here?
>>
>> It compiles without error when the value arg in the map is instead a
>> char or a char *.
>> std::map< int, char *> cm;

>
>You can not store an array in a map (or any other collection for that
>matter).


OK, thanks (and thank you all). Is your answer consistent with the
compiler telling me the map element, in this case, cm[0], is not an
l-value? It is an l-value, isn't it? It's the array that's not an
r-value, right?

Heck 11-18-2007 03:19 AM

Re: Re: behavior of a std::map I don't understand...
 
jeffrey.j.schmitz@gmail.com!
>On Nov 15, 11:41 pm, Erik Wikstrm <Erik-wikst...@telia.com> wrote:
>> On 2007-11-16 00:12, Heck wrote:
>>
>>
>>
>> > I'm using Visual Studio 2005, but i don't think that's (primarily)
>> > where I've gone wrong.

>>
>> > I was experimenting, trying, by the way, to move into writing my own
>> > iterator so that I can learn, ultimately, to write templates:
>> > std::map< int, char [12]> cm;
>> > char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};

>>
>> > cm[0] = str0;

>>
>> > The compiler says about that assignment,
>> > left operand must be l-value

>>
>> > What does this mean, what's happened here?

>>
>> > It compiles without error when the value arg in the map is instead a
>> > char or a char *.
>> > std::map< int, char *> cm;

>>
>> You can not store an array in a map (or any other collection for that
>> matter).
>>
>> --
>> Erik Wikstrm

>
>Wrap your array in a class and define the assignment operator


I'm getting to that, yes. Thanks very much.


All times are GMT. The time now is 04:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.