4 Replies - 34733 Views - Last Post: 30 July 2010 - 10:27 PM Rate Topic: -----

#1 ZlxA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-November 06

vector of objects

Posted 24 February 2007 - 08:14 PM

I'm doing a project and I have a vector of objects and I'm not exactly sure how to push items into the members of the object in the vector.

Stat Class:
 
class Stat 
{
public:
   unsigned int game;		// game number
   int pmRating; 			// plus minus rating in the game

   // Overloaded less than operator
   bool operator < (const Stat& s2) const {
	  return (pmRating < s2.pmRating);
   }
};



I have the following vector:
vector<Stat> record ;

So I figured to push items in it would be record.pmRating.push_back(itemtopush) but that didn't seem to work :P

Is This A Good Question/Topic? 0
  • +

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: vector of objects

Posted 24 February 2007 - 08:42 PM

Quote

So I figured to push items in it would be record.pmRating.push_back(itemtopush) but that didn't seem to work

Well you had the right idea, wrong concept.
when you defined vector<Stat> record; you were creating a vector of Stat objects. So if I had Stat MyStat; then I could append it to the vector with record.push_back(MyStat);. i.e you can only add the kind of object you declared the vector for. If you wanted an Vector of pmRatings you would need to use vector<int> either inside the class (if each stat can have more than one pmRating, or outside your code next to record.
Was This Post Helpful? 1

#6 ZlxA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-November 06

Re: vector of objects

Posted 24 February 2007 - 10:30 PM

View PostNickDMax, on 24 Feb, 2007 - 08:42 PM, said:

Quote

So I figured to push items in it would be record.pmRating.push_back(itemtopush) but that didn't seem to work

Well you had the right idea, wrong concept.
when you defined vector<Stat> record; you were creating a vector of Stat objects. So if I had Stat MyStat; then I could append it to the vector with record.push_back(MyStat);. i.e you can only add the kind of object you declared the vector for. If you wanted an Vector of pmRatings you would need to use vector<int> either inside the class (if each stat can have more than one pmRating, or outside your code next to record.


Oh somehow I misread that I was given a stat and not a pmRating to add to my vector
Was This Post Helpful? 0
  • +
  • -

#7 Guest_hagar54*


Reputation:

Re: vector of objects

Posted 30 July 2010 - 10:22 PM

View PostNickDMax, on 24 February 2007 - 07:42 PM, said:

Quote

So I figured to push items in it would be record.pmRating.push_back(itemtopush) but that didn't seem to work

Well you had the right idea, wrong concept.
when you defined vector<Stat> record; you were creating a vector of Stat objects. So if I had Stat MyStat; then I could append it to the vector with record.push_back(MyStat);. i.e you can only add the kind of object you declared the vector for. If you wanted an Vector of pmRatings you would need to use vector<int> either inside the class (if each stat can have more than one pmRating, or outside your code next to record.

Was This Post Helpful? 0

#8 Guest_Guest*


Reputation:

Re: vector of objects

Posted 30 July 2010 - 10:27 PM

NICKDMAX
Thank You, for 6 hours I have looked for an answer, you helped solve my very simple problem with an intelligent, simple answer.
Was This Post Helpful? 0

Page 1 of 1