Map Question

S

subaruwrx88011

I am really new to C++. Can I do this

typedef union
{
int i;
char* s;
float f;
}returnType;

typedef std::map<std::string, returnType> ItemStateMap;

ItemStateMap m_itemMap;

I am getting alot of compiler errors.
 
V

Victor Bazarov

I am really new to C++. Can I do this

typedef union
{
int i;
char* s;
float f;
}returnType;

typedef std::map<std::string, returnType> ItemStateMap;

ItemStateMap m_itemMap;

Is that all code there is? After adding proper #include's it compiled for
me without a hitch.
I am getting alot of compiler errors.

Like what? Is that a secret, and that's why you didn't post them?

V
 
I

int2str

I am really new to C++. Can I do this

Yes, but...
typedef union

In C++, just use:
union returnType...
{
int i;
char* s;

Use a std::string here. Or be very aware of how copying and destroying
a returnType item is going to work.
float f;
}returnType;

typedef std::map<std::string, returnType> ItemStateMap;

ItemStateMap m_itemMap;

I am getting alot of compiler errors.

For better help, please post a complete, minimal example program and
the compile errors you're getting.

Here's what I came up with that compiles:

#include <map>
#include <string>

union returnType
{
int i;
float f;
char *s; // <--- Replace this with a std::string!!
};

typedef std::map<std::string, returnType> ItemStateMap;

int main()
{
ItemStateMap itemMap;

returnType r;
r.i = 5;

itemMap["test"] = r;
}

Cheers,
Andre
 
P

Pete Becker

In C++, just use:
union returnType...




Use a std::string here.

You can't put an std::string in a union. It has a non-trivial default
constructor, a non-trivial copy constructor, a non-trivial destructor,
and a non-trivial assignment operator, any one of which is enough to
disqualify it.
 
S

subaruwrx88011

Hey thanks for all the replies. I found out my errors. They were
actually not in the declaration of the map or union. It was when I was
inserting into the map itself. Got it fixed. Thanks guys.
 

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

Similar Threads

Wierd Map Behavior 5
Seg Fault Help 14
Map Seg Fault 17
TF-IDF 1
Function traits 0
u16string,uchar16_t,,basic_ostream,basic_ostringstream not working 0
Assignment on map 6
Crossword 14

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,129
Latest member
FastBurnketo
Top