STL map

This is a discussion on STL map within the C++ Programming forums, part of the General Programming Boards category; This is a sample code i have, can anyone guide me how do i solve this problem, i have read ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    string is a type not var
    Code:
    string temp; //empty string
    apples.insert(temp, a);
    for example
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    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, 10:22 PM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 08:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21