I have declared an associative array and now want to print it:
#include <map>
#include <string>
#include <cstdio>
using namespace std;
int main() {
map<string, int> m;
m["Peter"] = 4;
m["John"] = 3;
m["Katie"] = 3;
map<string, int>::iterator curr,end;
for(curr = m.begin(), end = m.end(); curr != end; curr++) {
printf("%s : %i\n", curr->first, curr->second);
}
return 0;
}
I’m getting an error from my compiler:
main.cpp: In function ‘int main()’:
main.cpp:24: warning: cannot pass objects of non-POD type ‘const struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ through ‘...’; call will abort at runtime
And surprise suprise – it’s true – call aborts at runtime…
But I don’t know why… What should I fix? What exactly mean “through ‘…’” ?
try:
The
c_str()method convertstd::stringto a c-style string (char*).the
...refers to printf‘s vararg prototype:which won’t support passing c++ objects.
BTW, you should use
++currinstead ofcurr++. This is some c++ magic that prevent duplicating a reference.