Vector resize(unsigned int n, k=0)

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;
}
}
Do you want to preserve old values?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void IntVector::resize(unsigned int n, int k=0)
{
	if(n<=my_size)
		return;
	int* newArea = new int[n];
	for(int i = 0; i < my_size; ++i)
	{
		newArea[i] = data[i];
	}
	for(int i = my_size; i < n; ++i)
	{
		newArea[i] = k;
	}
	delete [] data;
	data = newArea;
	my_size = n;
}
Last edited on
Basically there are three resizes.

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.
So i should only initialize k to zero in the header file and exclude the initialization in the member functionarea?
You should include the k = 0 in both places.
Ok, much appreciated. Any idea why my code would work with a resize and input value, but wouldnt work with just a resize?
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.
Sorry STL . Link lists, deque , merge sorts, stacks, treemap , hashmap, and heap
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 ;)

STD -> an abbreviation for Sexually Transmitted Disease


ROFL. LOL
I love je LB, you're great comedian.
lol std;
Topic archived. No new replies allowed.