-1

I am trying to declare a vector in the constructor of a container class. The vector should be empty to start, I will use .push_back() in the main.cpp to add to it. Do I even need to set it to something in the constructor?

class Collection{
    private:
        vector<Card> setOfCards;
        int numCards;
        int maxSpend;
    public:
        Collection(int numCards, int maxSpend){
            this->numCards;
            this->maxSpend;
            setOfCards = new vector<Card>();
        }
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
lsmith61
  • 1
  • 2
  • 1
    Short answer: no. A vector will be initialized to empty by default, so if that's what you want, you don't need to do anything in the ctor to get it. – Jerry Coffin Feb 17 '22 at 01:47
  • Welcome to Stack Overflow. 1) If code is vital to your question, post it in the question. Not an image, not a link, not a link to an image, but the actual text. 2) A `vector` and a `vector*` are not the same thing. – Beta Feb 17 '22 at 01:48
  • You can't use `push_back` elsewhere in the code to add to a vector inside of a class unless you break encapsulation. That sentence raised a red flag. – chris Feb 17 '22 at 02:00
  • 1
    I suggest to read a book in the [list](https://stackoverflow.com/a/388282/4123703) since you have numbers of errors in this snippet. – Louis Go Feb 17 '22 at 02:04
  • In C++, members (and variables) are default initialized (using the default constructor, unless specified otherwise). This means you **should not** use `new`. The vector will be created with the object instance, and will be empty by default. You can simply start using `setOfCards` as soon as `Collection` is created without needing anything special. Newcomers to C++ should never need to use `new`, unless you understand what it entails. – fordcars Feb 17 '22 at 02:08
  • In the constructor, unless you have explicitly initialised `setOfCards` differently, it WILL have size of zero. – Peter Feb 17 '22 at 08:19

1 Answers1

1

Your class contains a member vector<Card> object, but new vector<Card> returns a vector* pointer to an object. You can't assign a vector* pointer to a vector object.

Nor do you need to. vector implements a default constructor, so your setOfCards member will be constructed automatically when the compiler enters your constructor.

That being said, your constructor is ignoring its input parameters. The statements this->numCards; and this->maxSpend; are no-op's.

The code should look more like this instead:

class Collection{
    private:
        vector<Card> setOfCards;
        int numCards;
        int maxSpend;
    public:
        Collection(int numCards, int maxSpend){
            // setOfCards has already been constructed by this point!
            this->numCards = numCards;
            this->maxSpend = maxSpend;
        }

Or, using a member initialization list:

class Collection{
    private:
        vector<Card> setOfCards;
        int numCards;
        int maxSpend;
    public:
        Collection(int numCards, int maxSpend) :
            /* setOfCards() is called implicitly here, */
            numCards(numCards),
            maxSpend(maxSpend)
        {
            // setOfCards has been constructed by this point!
        }

See Constructors and member initializer lists for more details.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770