0

I have a class Thing with a constructor Thing::Thing() and a method Thing::print(). I am trying to create arrayOfVectors such that each std::vector within the array is of size 0. The constructor function prints out the sizes of each vector correctly but the print() method does not.

I have tried calling arrayOfVectors[n].clear() and arrayOfVectors[n].assign(0,0) on each vector within the array but did not work.

Thing.hpp

class Thing {
private:
  std::vector<int>* arrayOfVectors;

public:
  Thing();
  void print() const;
};

Thing.cpp

Thing::Thing() {
  std::vector<int> arrayOfVectors[5];
  std::cout << arrayOfVectors[0].size() << std::endl; // 0
  std::cout << arrayOfVectors[1].size() << std::endl; // 0
  std::cout << arrayOfVectors[2].size() << std::endl; // 0
  std::cout << arrayOfVectors[3].size() << std::endl; // 0
  std::cout << arrayOfVectors[4].size() << std::endl; // 0
}

void Thing::print() const {
  std::cout << arrayOfVectors[0].size() << std::endl; // 0
  std::cout << arrayOfVectors[1].size() << std::endl; // 35183230189065
  std::cout << arrayOfVectors[2].size() << std::endl; // 33
  std::cout << arrayOfVectors[3].size() << std::endl; // 35
  std::cout << arrayOfVectors[4].size() << std::endl; // 108
}

main.cpp

int main() {
  Thing thing;
  thing.print();
  return 0;
}
7
  • 1
    Where are you assigning arrayofvectors to something? Commented Oct 15, 2018 at 0:19
  • @kingW3 It's at std::vector<int> arrayOfVectors[5];. There was a typo. Commented Oct 15, 2018 at 0:22
  • 1
    std::vector<int> arrayOfVectors[5]; creates a local variable in the constructor that no longer exists when the constructor finishes. This does not have any connection to the class member with the same name. Commented Oct 15, 2018 at 0:27
  • 1
    Possible duplicate of Declaring a local variable within class scope with same name as a class attribute Commented Oct 15, 2018 at 0:27
  • arrayOfVectors is a pointer that is not initialized when you call print() Commented Oct 15, 2018 at 0:29

1 Answer 1

3
Thing::Thing() {
  std::vector<int> store[5];
  ^^^^^^^^^^^^^^^^^^^^^^^^^

This here is an array of vectors. It is an automatic variable. Automatic variables are destroyed at the end of the block where they were created. In this case, the local array is destroyed at the end of the constructor call.

class Thing {
private:
  std::vector<int>* arrayOfVectors;
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This here is a pointer to a vector. It is not an array. It is a member variable of the class Thing. It is a completely separate variable from the local store variable.

Your constructor never initializes the member variable, so when you indirect the pointer in the print member function, the behaviour of the program is undefined.


If you want your class to have an array as a member variable, you can write it like this:

class Thing {
private:
  std::vector<int> arrayOfVectors[5];

You don't need to declare a constructor, since the automatically generated one does exactly what you want - all vectors will be empty.


How can i avoid setting a specific number like 5 in the header definition?

You cannot avoid that with an array variable. The size must be known at the time of compilation. If you need an array with non-fixed size, you need to allocate the array in the dynamic storage. The idiomatic way to create a dynamic array is to use std::vector:

class Thing {
private:
  std::vector<std::vector<int>> vectorOfVectors{5};
Sign up to request clarification or add additional context in comments.

8 Comments

How can i avoid setting a specific number like 5 in the header definition?
after declaring the vectorOfVectors in the header, how do you assign std::vector<std::vector<int>> to vectorOfVectors in the constructor definition
@Senyokbalgul you don't need to assign anything to vectorOfVectors if you use a default member initializer like I've shown in my answer (nor do you need to declare a constructor).
But don't use 2D vectors.
@LightnessRacesinOrbit Why not?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.