-
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!
-
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
-
ahh ok, i was just hoping to reduce the size of my code! Thanks!
-
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 ) );
-
That is indeed a form of table.
--
Mats
-
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
-
Sounds like you forgot to #include <map>.
-
oho! so i did!
how could i use this to help me?