I am practicing classes so I I tried to make a class about an animal, where the private attributes are the animals age, number of previous owners, and a sequence of strings that holds the names of the previous owners. I made the class and it compiles by itself, but when I try to set a previous owner I get an error for segmentation fault core dump, and when I try to call the copy assignment or copy constructor I get an error bad memory allocation. I tried to debug and figure out where I am going wrong with the memory but I can't figure it out.
These are just the errors I found so far and trying to fix, I am using the flag -std=c++14 when I compile in Ubuntu for nullptr.
The use of std::vector is use of dynamic memory. The difference is that if you do it yourself, then you have to know what you are doing, but with the vector somebody else has already done most of the hard work for you.
What is thaught to beginners is not "modern C++". It is "thinking". You are given very simple (Lego/Minecraft) building blocks and you have to build complex models. You are forced to think to get things right. Modern C++ assumes that you can already think and simply provides more elaborate blocks to make the "work" easier.
i Know how to use vectors, I am trying to learn how to use pointers in c++ so that I can understand how c++ manages memory, that's why I am trying to figure out how to make this work with pointers.
In the constructor Animal::Animal(constint &a, constint &n) you never set the object's previousOwners variable.
You are creating a whole new variable of that name, and making it point to an array. Then, the constructor ends, that whole new variable is discarded (memory leak) and the object's internal previousOwners variable hasn't been touched, so it will be pointing into random memory.
See also Animal::Animal(const Animal & copyFrom) and Animal::operator=(const Animal & copyAssignFrom) ; creating a whole new previousOwners pointer, doing things to it, and then discarding it, leaving the object's own internal previousOwners variable untouched.
When you build, also use the flag -Wall and -Wextra and -Wshadow (and probably more too). -Wshadow would have warned you that you were shadowing those variables by creating new ones of the same name.
@Repeater, thank you, I forgot that I wasn't suppose to put std::string* before the previous owners because this means it was creating a new array and not using the array from private, I fixed all of the instances and it works now.