behavior of a std::map I don't understand...

H

Heck

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
 
R

red floyd

Heck said:
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.
 
G

Guest

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).
 
J

Juha Nieminen

Heck said:
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";
 
L

LR

Erik said:
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
 
V

Victor Bazarov

LR said:
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
 
J

jeffrey.j.schmitz

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).

Wrap your array in a class and define the assignment operator

Jeff

http://theschmitzer.blogspot.com
 
G

Guest

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.
 
H

Heck

Erik Wikström!
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?
 
H

Heck

(e-mail address removed)!
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).

Wrap your array in a class and define the assignment operator

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top