Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,489 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,296 people online right now. Registration is fast and FREE... Join Now!




vector of objects

 
Reply to this topicStart new topic

vector of objects

ZlxA
24 Feb, 2007 - 07:14 PM
Post #1

New D.I.C Head
*

Joined: 6 Nov, 2006
Posts: 10


My Contributions
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:
CODE

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 tongue.gif

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Vector Of Objects
24 Feb, 2007 - 07:42 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
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.
User is online!Profile CardPM
+Quote Post

ZlxA
RE: Vector Of Objects
24 Feb, 2007 - 09:30 PM
Post #3

New D.I.C Head
*

Joined: 6 Nov, 2006
Posts: 10


My Contributions
QUOTE(NickDMax @ 24 Feb, 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.


Oh somehow I misread that I was given a stat and not a pmRating to add to my vector
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:15PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month