CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2006
    Posts
    55

    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

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: STL map declaration

    when i try to link the project it is giving me syntax error near the extern.
    What syntax error?

    - petter

  3. #3
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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()!

  4. #4
    Join Date
    Feb 2006
    Posts
    55

    Unhappy 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

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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());

  6. #6
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    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

  7. #7
    Join Date
    Feb 2006
    Posts
    55

    Resolved 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
  •  





Click Here to Expand Forum to Full Width

Featured