|
-
February 21st, 2006, 07:47 AM
#1
STL map declaration
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
-
February 21st, 2006, 08:13 AM
#2
Re: STL map declaration
when i try to link the project it is giving me syntax error near the extern.
What syntax error?
- petter
-
February 21st, 2006, 08:21 AM
#3
Re: STL map declaration
Be sure to include <map> and <string> in the header, before the declaration.
And, use explicit scope resolution:
Code:
extern std::map<std::string,std::string> gsObjForPrimaryMap;
And, remove, if there are any, all the using directives off your header!
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
February 21st, 2006, 08:25 AM
#4
Re: STL map declaration
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
-
February 21st, 2006, 08:54 AM
#5
Re: STL map declaration
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.
Code:
printf("%s",(*itr).first.c_str());
printf("\t%s\n",(*itr).second.c_str());
-
February 21st, 2006, 08:54 AM
#6
Re: STL map declaration
The printf %s expects a plain old (zero ended) c-string, not a c++ std::string:
Code:
for(itr = gsObjForPrimaryMap.begin();itr!=gsObjForPrimaryMap.end();itr++)
{
printf("%s",(*itr).first.c_str());
printf("\t%s\n",(*itr).second.c_str());
}
- petter
-
February 22nd, 2006, 12:03 AM
#7
Re: STL map declaration
Thanks much the function .c_str() solved my problem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|