Thread: Objects

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    Objects

    im stuck with objects
    Code:
    class solar
    
    { 
       int satellites;
    }earth, venus;
    
    int main()
    {
        string planet;
        cout << "enter a planet name";
        getline(cin, planet);
       cout << planet << " has " << earth.satellites << " satellites"; /*i would like to reference the object name using the string variable, i.e planet.satellites*/
    }
    thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That isn't possible in C. You could make a table of objets, and store the name of a planet in each class object, and then search for the matching name in a table.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    5
    ahh ok, i was just hoping to reduce the size of my code! Thanks!

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What about using a map?
    Code:
    std::map<std::string, solar> planets;
    planets.insert( std::make_pair( "Earth", earth ) );
    planets.insert( std::make_pair( "Venus", venus ) );
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is indeed a form of table.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    5
    Hi, i forgot about this problem, and now ive run into it again someone else pointed me in this direction too, but im having problems with maps. is there anyway you could show me how to implement them in this scenario? im getting compiler errors with this

    Code:
    std::map<std::string, solar> planets;
    planets.insert( std::make_pair( "Earth", earth ) );
    planets.insert( std::make_pair( "Venus", venus ) );
    ('map' is not a member of 'std')

    p.s bump

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Sounds like you forgot to #include <map>.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    5
    oho! so i did!

    how could i use this to help me?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 07:37 AM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM