Std Map .. Help

Discussion in 'C++' started by ash, Apr 20, 2004.

  1. ash

    ash Guest

    Hi,

    I'm getting started with STL, and am stuck at creating a map
    container. I checked one of the texts and found a code in there. To
    make it simple, i wrote the following:

    #include <iostream.h>
    #include <string.h>
    #include <map>

    typedef std::map<string,string,std::less<string>> mymap;

    int main()
    {
    mymap somemap;
    return 0;
    }


    This gives me 6 error messages, an a warning. Could anyone point me
    the mistake....i've been to grasp the topic for two days now!

    Initially i thought i might not be using string properly, so i changed
    the code typedef to

    typedef std::map<int,int, std::less<int>> mymap;

    This give me an error saying :
    C:\Documents and Settings\Ash\Desktop\Maps\MuliMap\Main.cpp(4) : error
    C2146: syntax error : missing ',' before identifier 'mymap'
    C:\Documents and Settings\Ash\Desktop\Maps\MuliMap\Main.cpp(4) : :
    error C2065: 'mymap' : undeclared identifier
    C:\Documents and Settings\Ash\Desktop\Maps\MuliMap\Main.cpp(4) : error
    C2143: syntax error : missing '>' before ';'
    C:\Documents and C:\Documents and
    Settings\Ash\Desktop\Maps\MuliMap\Main.cpp(4) : warning C4091:
    'typedef ' : ignored on left of 'class std::map' when no variable is
    declared


    Thanks,

    Ash
     
    ash, Apr 20, 2004
    #1
    1. Advertising

  2. <ash> wrote in message news:...
    > Hi,
    >
    > I'm getting started with STL, and am stuck at creating a map
    > container. I checked one of the texts and found a code in there. To
    > make it simple, i wrote the following:
    >
    > #include <iostream.h>


    Non-standard header file, use <iostream> (without a .h)

    > #include <string.h>


    This is a valid header file, but it doesn't declare the std::string class
    which is what you are thinking it does, instead it declares the C string
    handling routines. Use <string> instead (again without a .h)

    > #include <map>
    >
    > typedef std::map<string,string,std::less<string>> mymap;


    std::less is unecessary, but string is wrong, it should be std::string. Try
    this

    typedef std::map<std::string,std::string> mymap;

    >
    > int main()
    > {
    > mymap somemap;
    > return 0;
    > }
    >
    >
    > This gives me 6 error messages, an a warning. Could anyone point me
    > the mistake....i've been to grasp the topic for two days now!


    Don't know what book you are reading, doesn't it have any syntactically
    valid programs in it?

    You need to use the correct header files (no C++ header files have .h in
    them)
    You need to remember std::
    You should forget about std::less (at least for now).

    john
     
    John Harrison, Apr 20, 2004
    #2
    1. Advertising

  3. ash

    Artie Gold Guest

    ash wrote:
    > Hi,
    >
    > I'm getting started with STL, and am stuck at creating a map
    > container. I checked one of the texts and found a code in there. To
    > make it simple, i wrote the following:
    >
    > #include <iostream.h>

    #include <iostream>
    > #include <string.h>

    #include <string>

    [<string.h> is a C header that contains the prototypes for things like
    strcmp(), strcpy(), and the like -- *not* C++ std::string]

    > #include <map>
    >
    > typedef std::map<string,string,std::less<string>> mymap;

    typedef std::map<std::string,
    std::string,
    std::less<std::string> > mymap;

    [the space between the enclosing `>'s is necessary]
    >
    > int main()
    > {
    > mymap somemap;
    > return 0;
    > }
    >
    >
    > This gives me 6 error messages, an a warning. Could anyone point me
    > the mistake....i've been to grasp the topic for two days now!
    >
    > Initially i thought i might not be using string properly, so i changed
    > the code typedef to
    >
    > typedef std::map<int,int, std::less<int>> mymap;
    >
    > This give me an error saying :
    > C:\Documents and Settings\Ash\Desktop\Maps\MuliMap\Main.cpp(4) : error
    > C2146: syntax error : missing ',' before identifier 'mymap'
    > C:\Documents and Settings\Ash\Desktop\Maps\MuliMap\Main.cpp(4) : :
    > error C2065: 'mymap' : undeclared identifier
    > C:\Documents and Settings\Ash\Desktop\Maps\MuliMap\Main.cpp(4) : error
    > C2143: syntax error : missing '>' before ';'
    > C:\Documents and C:\Documents and
    > Settings\Ash\Desktop\Maps\MuliMap\Main.cpp(4) : warning C4091:
    > 'typedef ' : ignored on left of 'class std::map' when no variable is
    > declared
    >


    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
     
    Artie Gold, Apr 20, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Hildebrand
    Replies:
    5
    Views:
    8,191
    krogers
    Mar 20, 2012
  2. Peter Jansson
    Replies:
    5
    Views:
    6,425
    Ivan Vecerina
    Mar 17, 2005
  3. Replies:
    1
    Views:
    451
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,049
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    2,066
    James Kanze
    Dec 21, 2008
Loading...

Share This Page