I have my resize function working if two arguments are
given( resize(4,9)). However when the compiler reaches a resize with
only one argument, it fails (resize(3)). I thought that the point of k
being initialized to 0 was to avoid this issue. Below is my code. Any
suggestions?
void IntVector::resize(unsigned int n, int k=0)
{
if(n<=my_size)
return;
if(n > my_size)
{
reserve(my_capacity+n);
}
int temp=my_size;
my_size=n;
for(int i = temp; i < my_size; i++)
{
data[i] = k;
}
}
Resize(4,9) // resizes to 4 and will add the value 9 to the empty slots of the vector. If the vector already has values then nothing is changed except the size.
Resize(3) // one argument that changes size to 3. Values should be 4,4,4
Resize(5) // should resize to 5 and have two empty slots. Those slots should then be zero. Vector should read 4,4,4,0,0
Resize(7,9) // should resize vector to 7 and add 9 to extra two slots. Vector should now read 4,4,4,0,0,9,9
The k=0 part should be in the header where the member function is declared in the class scope. Putting that in the actual definition itself is useless because it is not visible to the calling code.
The deafult values feature is a compile-time feature where the compiler automagically supplements the default values into the function call for you, in reality the function will always be called with two arguments.
Because of what I just explained a few posts ago; the = 0 part was not visible to the compiler when it checked that you were passing the correct number of parameters to the function (it was in an entirely different .cpp file).
Awesome. If i have issues and the future and you wanted to take the time to explain certain things to me or help me out with issues, i would consider hiring you as a tutor. That is, if i am so lost i feel i will fail the class. Dealing with data structures and STD.
std -> a namespace and an abbreviation for STanDard
STL -> an abbreviation for Standard Template Library
...
STD -> an abbreviation for Sexually Transmitted Disease
Don't forget you are allowed to edit and delete your posts ;)