Click to See Complete Forum and Search --> : STL map declaration
sspr
February 21st, 2006, 06:47 AM
hi
I need to declare a map globally and use it all over my project. I am declaring the map in a header file like this
map<string,string> gsObjForPrimaryMap;
and in another file i am using it as follows
extern map<string,string> gsObjForPrimaryMap;
typedef pair<string,string> mypair;
when i try to link the project it is giving me syntax error near the extern.
If i have to declare a map globally and use it am i doing the correct thing if not please correct me
thanks in advance
wildfrog
February 21st, 2006, 07:13 AM
when i try to link the project it is giving me syntax error near the extern.
What syntax error?
- petter
SuperKoko
February 21st, 2006, 07:21 AM
Be sure to include <map> and <string> in the header, before the declaration.
And, use explicit scope resolution:
extern std::map<std::string,std::string> gsObjForPrimaryMap;
And, remove, if there are any, all the using directives off your header!
sspr
February 21st, 2006, 07:25 AM
I am sorry.... that was my mistake i could resolve it.
but now i have new problem i am not able to display the contents of the map.
i declared an iterator for the map like this
map<string,string>::iterator itr
and in my function i have wriiten a for loop as follows
for(itr = gsObjForPrimaryMap.begin();itr!=gsObjForPrimaryMap.end();itr++)
{
printf("%s",(*itr).first);
printf("\t%s\n",(*itr).second);
}
i am not getting any complie error. I am getting a run time error. I inserted a breakpoint and tried debugging i noticed that the pointer itr address is getting messed up some where
Philip Nicoletti
February 21st, 2006, 07:54 AM
printf does not know how to print a std::string directly. Use the
std::string member fundtion c_str() to get something that
printf understands.
printf("%s",(*itr).first.c_str());
printf("\t%s\n",(*itr).second.c_str());
wildfrog
February 21st, 2006, 07:54 AM
The printf %s expects a plain old (zero ended) c-string, not a c++ std::string:
for(itr = gsObjForPrimaryMap.begin();itr!=gsObjForPrimaryMap.end();itr++)
{
printf("%s",(*itr).first.c_str());
printf("\t%s\n",(*itr).second.c_str());
}
- petter
sspr
February 21st, 2006, 11:03 PM
Thanks much the function .c_str() solved my problem
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.