Custom vector class

I have written a custom vector class for an assignment, and am having trouble with the driver code. I created the push_back function the first code. Then in the second is the first test with the push_back. I can't seem to make both driver codes accurate.

Either the first one prints out an extra number (i think an address) or the size is not accurate on the second driver code. I can't seem to find out where this error is happening. I would really appreciate any advice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
void MyVector::push_back(int n)
{
	if (capacity1 < size1 + 1)
	{
		capacity1 = size1 * TWO;
		 int* temp = new int[capacity1];
		
		for (int i = 0; i < size1; i++)
		{
			temp[i] = myArray[i]; 	
		}
		temp[size1] = n; 
		size1++; 
		delete[] myArray;
		myArray = temp;
	}
	else
	{
		myArray[size1] = n;
		size1++; 	
	}
	 
}


first driver code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
int main{
	cout << "\nPushing three values into sam";
	sam.push_back(21);
	sam.push_back(31);
	sam.push_back(41);
	cout << "\nThe values in sam are: ";
	for (int i = 0; i < sam.size() + 1; i++)
	{
		try
		{
			cout << sam.at(i) << " ";
		}
		catch (int badIndex)
		{
			cout << "\nOut of bounds at index " << badIndex << endl;
		}
	}


//second driver code: 

        sam.clear();
	cout << "\nsam has been cleared.";
	cout << "\nSam's size is now " << sam.size();
	cout << "\nSam's capacity is now " << sam.capacity() << endl;
	cout << "---------------\n";
	cout << "\nPush 12 values into sam.";
	for (int i = 0; i < 12; i++)
		sam.push_back(i);

	cout << "\nSam's size is now " << sam.size();
	cout << "\nSam's capcacity is now " << sam.capacity() << endl;
	cout << "---------------\n";

	cout << "\nTest to see if contents are correct...";
	for (int i = 0; i < sam.size(); i++)
	{

		cout << sam.at(i) << " ";
	}
	cout << "\n--------------\n";
	cout << "\n\nTest Complete...";
	cout << endl;
	system("PAUSE");
	return 0;
}
Last edited on
Please format your code with the tags.
I'm sorry, is that the right way to use tags?
Where do you define "sam"? Global variables are generally bad (I am assuming that it's a global variable since you don't declare it in main).

Also this might just be that you didn't copy it correctly, but main is a function and needs parenthesis before the function block.

What does your class definition look like? Does your size() function simply return size1?
Last edited on
I define Sam in my main, I didn't copy it right. Main is also deflated with parenthesis, I meant to correct it.

I declared sam in the main as well.

 
MyVector sam;

Size does just return size1.
for (int i = 0; i < sam.size() + 1; i++)
Why + 1?
Take into consideration an empty "sam".
Size should be 0.
If you add a 1, you are "faking" an element which is not there.
If you want the exception to be thrown and handled, show us the MyVector::at code.
The problem should be there.
Last edited on
I'm not sure exactly why we use size+1 . The driver code is provided for us.

the code to my at function is:

1
2
3
4
5
6
7
8
9
10
11
12

int MyVector::at(int i)
{

	if (size1 < i)
	{
		throw i; 
	}
	return myArray[i]; 
	
	
}
Last edited on
Okay but is it fixed if you change your for loop to go to just size() instead of size() + 1?
Oh yeah! And because of you I know more what i'm looking for! Thank you so much! It uses size + 1 so it will throw an exception. So I have figured out the program is working except i'm not catching the exception correctly.
No, I think they want him to throw an exception, since there's a try/catch block.
Anyways, yes, you'll have to work on MyVector::at.

Consider a MyVector::at call like the following:
1
2
MyVector empty;
empty.at(0);


Will the exception be thrown? Why?
-whoops, just noticed you posted before me-
Last edited on
Topic archived. No new replies allowed.