Errors with maps.

"
map<string,armour> char_armour;
char_armour["head"]=a_GetNull;
" ends up producing.

D:\dos\numberstest\surefirewaytodie2.cpp|202|error: 'char_armour' does not name a type|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 1 seconds) ===|

You can download the full code here:
puu.sh/4WDIO.cpp
Just so I can prove that I included the proper headers.

I'm using CodeBlocks.
And yes, "armour" is a class.
char_armour["head"]=a_GetNull;

You are doing this inside a class body. You can't do that.... it has to be done in a function body.
But otherwise, how can I declare the various equipment slots to the character?
I tried with pointers, but that would be problematic if I play it "safe" (using private variables, then constructors....)
Your code will work... it just has to go somewhere that code can execute. Class bodies are for declaring things... they are not for executable code.

Move your code somewhere that code can actually run (ie: in a function). In this case... it would be appropriate to move this code to the class's constructor.
Won't work either.

It gives me this:
D:\dos\numberstest\surefirewaytodie2.cpp||In constructor 'chr::chr()':| D:\dos\numberstest\surefirewaytodie2.cpp|201|error: invalid types 'std::map<std::basic_string<char>, armour>*[const char [5]]' for array subscript| ||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
Are you going to show us the code that's generating the error? Or are we just supposed to guess what it is you might have done to cause it?
Ah yes, sorry.
Here it is:
puu.sh/4XgDm.cpp
Posting the code here would've been better.

char_armour is a pointer which points to a random place in memory. You may not dereference it or index it as if it were an array with a pointer to const char.

Since there is no reason for it to be a pointer, why have you made it a pointer?

[Edit: And one wonders why you aren't attempting to initialize the other 35ish class variables in the constructor. One also wonders at the design that allows 35ish variables in a single class.]
Last edited on
The code is still under construction.
The point is, there is the weapon class. There will be iron swords, steel swords, etc.
And there will be the characters, who will... you know, have those weapons.
There is an object call eq_wpn (equipped weapon), which obviously takes all the variables of a certain weapon....
Then there are armour too, and....

Anyway, the point is, that a friend told me to use maps and dictionaries instead of "the 5000 pointers" (which are most probably troublesome anyway.... I'm not even sure that objects can be even equated)
I recently contacted her that the maps she adviced me are having all sorts of actions, so it would most likely wiser for me to stick to the pointer objects.
Download my code and try to compile it. See the error for yourself.
This map thing is a bloody joke..................

I replaced the string and the armour-object both with integers, and it worked fine.
So it apparently has problems with me using a string as the key, and an object as the value.

I'm sticking to my 5000+ pointers, even if other programmers call it a "bad practice". A bad practice it may be, it's at least functional.
Unlike the "map" thing a more experienced comrade suggested... What a joke.... what a waste.
I replaced the string and the armour-object both with integers, and it worked fine.
So it apparently has problems with me using a string as the key, and an object as the value.


As I said before, you didn't actually have a map. You had an uninitialized pointer to map.
Okay, I got this:

https://github.com/Metalhead33/Faulty-code./blob/master/.thecode

And the result is:

http://puu.sh/4Y6HW.png
This only happens when I declare the chr and when I have the equipment declared (armour, weapons)
When I remove either, the error does not come up.
std::map::at requires the key to already exist in the map. The key doesn't exist in the map. Don't use at.

There is no reason to use a pointer to map here or new. Just use a map.

Look up the semantics of map methods before you use them.
Last edited on
Topic archived. No new replies allowed.