I'm trying to create a custom vector class.
For some reason I can't seem to get any output from my function calls and I'm not sure if it's because the vector array isn't being properly assigned values, or if the vector.at function isn't outputting correctly. It's compiling just fine at the moment. What's wrong with my code?
Can't really say what is wrong with your code, except that you are doing a lot of allocation in the constructor. From what you have posted, here is what I came up with:
Why and how do I fix it? Even if I comment out the line with new entirely I still get no output. Even when I create a test function that uses data entirely separate from *elements I still get no output. and it still compiles just fine no matter how i do it.
The at function is clearly supposed to output the value at elements[index] (see "return elements[index]")
If by, "clearly supposed to output the value" you mean is "clearly supposed to return the value," I would agree with your assessment. There is no "output" involved here.
1 2 3 4 5 6 7 8 9
#include <iostream>
// ...
int main()
{
// ...
std::cout << "The value returned for V2.at(3) is " << V2.at(3) << '\n' ;
// ...
}
I will grant that that was a silly mistake.... point still stands, the MyVector(int size) function isn't initializing any elements in the elements array, regardless of the presence of new or realloc....
It would be silly to test that assumption if you're using realloc in combination with new. Assuming that you aren't using realloc, what makes you think it isn't assigning values to elements of your elements array?
Real quick, what are you doing in the function definition after the arguments with
"T default_value =T())
: vtr_size(size), capacity(size), elements(new T[size])"
We haven't gone over anything like that in class, is that just the same as saying vtr_size = size, etc? And what is default_value even defined as here??
Edit: okay default_value is less confusing, but using the () op after T in the definition is still weird seeming to me. Oh wait, I'm being an idiot. Thanks for the very good reference cire.
Real quick, what are you doing in the function definition after the arguments with
1 2
T default_value =T())
: vtr_size(size), capacity(size), elements(new T[size])"
This is the constructor initialization list. It is how one initializes class members (as opposed to assigning to members in the body of the constructor.) For the purposes of this code, they are pretty much equivalent, but one should prefer to use the initialization list.
T default_value = T() sets default_value to a default constructed T (which would be 0 for an integral type) if a value isn't supplied when the constructor is called.
Alright, I kinda hate coming back for more when I'm making so much progress, but I'm a little stuck on my pop_back function. The problem is that when I use "free" and "realloc" on elements to reduce it's actual capacity by 1 the first few memory locations have already been overwritten by the time realloc is done.
To get around this I simply make a temporary array with the values of element I want to keep, and recopy its information after reallocation is complete. However I seem to be running into an infinite loop or something because the program stops responding.
Fixed it somewhat, but it still crashes. If I free (temp) it crashes at the same spot, if I free and delete it crashes at the same spot, if I only delete it crashed later during runtime, if I neither free nor delete it crashes when the program closes. Whats wrong now?