Problem using struct as std::map value type
-
Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.
-
Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.
hello i am not sure you can put a struct into a map. if i were you i would change the struct into a class with a copy constructor regards
-
Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.
You can't use aggregate syntax (that's the curly bracket stuff) anywhere except a struct/array variable initialiser. However, you certainly can put a struct in a
map
. Try the code below - it declares and initialises anSRect
variable which it then inserts into the map:SRect sRect = {iX, iY, iW, iH}; RectMap.insert(make_pair(iID, sRect));
-
Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.
What is your map definition? is it
map<int, SRect *> RectMap;
or
map<int, SRect> RectMap;
In the first case, you'll have to insert the structure variable into the map as below :
RectMap\[0\] = new SRect();
or
SRect s;
RectMap[0] = &s;i.e. before accessing the variable from map, make sure you've inserted it. And as you're saying RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); gives compiler error, I'm pretty much sure that your map declaration is as :
std::map<int, SRect \*> RectMap;
Better this will help you more...
[Delegates] [Virtual Desktop] [Tray Me !]
-Malli...! :rose:**** -
Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.
I've few confusions. It's possible to store structure as a value inside map.
std::map<int,srect> mymap;
Austrian_Programmer wrote:
void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; }
As per your code, you created the map as
std::map<int,srect*> mymap;
Probably you might be accessing an NULL or invalid pointer.-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts
-
I've few confusions. It's possible to store structure as a value inside map.
std::map<int,srect> mymap;
Austrian_Programmer wrote:
void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; }
As per your code, you created the map as
std::map<int,srect*> mymap;
Probably you might be accessing an NULL or invalid pointer.-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts
I found out that if I just use the struct and not struct pointers as map value type it's working without making any problems. Anyway, I don't understand why. In the meantime I also found a few code examples using a struct or a class as map value type, but I didn't see any examples using struct pointers.
-
I found out that if I just use the struct and not struct pointers as map value type it's working without making any problems. Anyway, I don't understand why. In the meantime I also found a few code examples using a struct or a class as map value type, but I didn't see any examples using struct pointers.
I asked you regarding structure pointer as value because in the function you've mentioned using -> operator instead of . (dot) operator to access the members.
-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts
-
Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.
map RectMap; <- error typedef unsigned key_type; <- any type you want std::map<key_type, SRect> RectMap; <- OK SRect sr = {iX, iY, iW, iH}; <- should work RectMap[iID] = sr; OR RectMap[iID] = {iX, iY, iW, iH}; <- should work Only write something like RectMap[iID]->iX if you are storing pointers to type sRect in the map; which would be bad and stuped unless you have a really good reason for doing so. If you are storing pointers ( std::map<key_type, SRect*> ) in the map, then you will need to write a method to free the memory pointed to by all those elements before you allow the map to be destroyed. If you are storing the actual data instead ( std::map<key_type, SRect> ), then the clean up will be automatic when the map is destroyed. To recap: ... BAD ...
std::map<key_type, SRect*> RectMap; <- bad
RectMap[key] = new SRect(iX, iY, iW, iH); <- this is why it is bad
void cleanup_before_destruction() <- and this is why it is bad
{
std::map<key_type, SRect*>::iterator i;
for( i = RectMap.begin(); i != RectMap.end(); ++i )
delete *i;
}... GOOD ...
std::map<key_type, SRect> RectMap; <- good
SRect sr(iX, iY, iW, iH);
RectMap[key] = sr;INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra