Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create objects while adding them into a vector?

Tags:

c++

I have a C++ vector. I want the vector to hold a variable number of objects.

Visual Studio 2012 is giving me an error:

Error: type name is not allowed 

From this C++ code:

#include <iostream> #include <vector> using namespace std;  class testObject{ private:    int someInt; public:    testObject(int a){ someInt=a; }    void show() { cout<<someInt<<endl; } };  int main() {     vector<testObject> testVector;     cout << "Initial size: " << testVector.size() <<endl;      for ( int i = 0; i < 3; i++ )         testVector.push_back(testObject(3));     cout << "New size: " << testVector.size() << endl;      for ( int j = 0; j < 3; j++ )         testVector[ j ].show();      system("pause"); }     

But here's another sample of code that looks the same but it's not working.

void Dealer::setNumberOfPlayers( const int tNumber ) {     for ( int i = 0; i < tNumber; i++ )         vectorOfGamers.push_back(Player); // Player is a class that I created } 

Can I create vector to hold objects of Dealer, Bot and Player at the same time? How do I do that? As I know, all objects in vector should be of one type.

like image 630
eoLithic Avatar asked Apr 04 '13 03:04

eoLithic


People also ask

How do you add elements to a vector set?

Get the vector to be converted. Create an empty set, to store the result. Iterate through the vector one by one, and insert each element into the set. Print the resultant set.

How do I add something to a vector in C++?

Appending to a vector means adding one or more elements at the back of the vector. The C++ vector has member functions. The member functions that can be used for appending are: push_back(), insert() and emplace(). The official function to be used to append is push_back().

Can you put a class in a vector?

Yes you can. You can create a vector of pointers that points to the base class Gamer .


1 Answers

To answer the first part of your question, you must create an object of type Player before you can use it. When you say push_back(Player), it means "add the Player class to the vector", not "add an object of type Player to the vector" (which is what you meant).

You can create the object on the stack like this:

Player player; vectorOfGamers.push_back(player);    // <-- name of variable, not type 

Or you can even create a temporary object inline and push that (it gets copied when it's put in the vector):

vectorOfGamers.push_back(Player());    // <-- parentheses create a "temporary" 

To answer the second part, you can create a vector of the base type, which will allow you to push back objects of any subtype; however, this won't work as expected:

vector<Gamer> gamers; gamers.push_back(Dealer());    // Doesn't work properly! 

since when the dealer object is put into the vector, it gets copied as a Gamer object -- this means only the Gamer part is copied effectively "slicing" the object. You can use pointers, however, since then only the pointer would get copied, and the object is never sliced:

vector<Gamer*> gamers; gamers.push_back(new Dealer());    // <-- Allocate on heap with `new`, since we                                    // want the object to persist while it's                                    // pointed to 
like image 81
Cameron Avatar answered Sep 25 '22 17:09

Cameron