Thread: STL map

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    7

    STL map

    This is a sample code i have, can anyone guide me how do i solve this problem, i have read alot of maps, but i need help on how to get it right.

    Code:
    // There is Apple class
    //Fruit.h
    
    std::map<std::string,Apple> apples;
    
    // I have this method in, Fruit.cpp
    
    //Fruit.cpp   
    //have to use insert() method of map
    
    void Fruit::addApple(const Apple& a){
    		
               apples.insert(std::pair<string, Apple>);
    
    
    // error: expected primary-expression before ')' token 
    
    //create apple and use 'other' addApple method
    
    void Fruit::addApple(const string id, const int duration){
    		
    		       Apple(id, duration); 
    		       addApple(Apple);
    
    // error: expected primary-expression before ')' token

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    Apple(id, duration); 
    addApple(Apple);
    1st line constructs an object and throughs it away
    2nd line tries to pass class name instead of the reference to the object

    I suppose

    addApple(Apple(id, duration));
    should work

    same goes for the other error
    std::pair<string, Apple> is just a type, you should pass a reference to an object
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    7

    help

    how do i pass a reference to a object:

    void Fruit::addApple(const Apple& a){
    apples.insert(string, &a); //error: expected primary-expression before ',' token

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    string is a type not var
    Code:
    string temp; //empty string
    apples.insert(temp, a);
    for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are using a string as the key to the map. What does that string represent? Is that an id?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Problem with static STL i.e. map
    By vijay_choudhari in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2007, 05:56 AM
  3. STL Map Object
    By nomes in forum C++ Programming
    Replies: 6
    Last Post: 09-11-2003, 01:51 PM
  4. Unsorted Map and Multimap :: STL
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2002, 11:22 PM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:11 AM