Overloading operators

I have a program that is giving me some unexpected data in the output window, and I am not sure why it is there. I will include all code so you can run it for yourself. The output leaves a -842150451 multiple times after the work is done in the ouput. Thanks in advance.

vectorHeader.h
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>

using namespace std;

//Declaring constant
const int VECTOR_CAP = 2;

class myVector
{
private:
	//Setting data members
	int* vectorData;
	int cap;
	int numElements;

public:
	//Default constructor
	//Purpose: Creates a vector
	//Parameters: None
	//Returns: None
	myVector();

	//Parameterized constructor
	//Purpose: Creates a vector capacity of n
	//Parameters: None
	//Returns: None
	myVector(int n);

	//Copy Constructor
	//Purpose: Copy data into vector
	//Parameters: myVector object
	//Returns: None
	myVector(const myVector& copy)
	{
		cap = copy.cap;
		vectorData = new int[cap];
		for (int i = 0; i < cap; i++)
		{
			this->vectorData[i] = copy.vectorData[i];
		}
	}

	//Destructor
	//Purpose:Deletes any dynamically allocated storage
	//Parameters: None
	//Returns: None
	~myVector();

	//Size function
	//Purpose: returns the size of your vector
	//Parameters: None
	//Returns: The size of your vector as an integer
	int size() const;

	//Capacity function
	//Purpose: Returns the capacity of the vector
	//Parameters: None
	//Returns: Maximum value that your vector can hold
	int capacity() const;

	//Clear function
	//Purpose: Deletes all of the elements from the vector and resets its size to zero and its capacity to two; thus becomming empty
	//Parameters: None
	//Returns: None
	void clear();

	//push_back function
	//Purpose: Adds the integer value n to the end of the vector
	//Parameters: Takes a integer to be placed in the vector
	//Returns: None
	void push_back(int);

	//at function
	//Purpose: Returns the value of the element at position n in the vector
	//Parameters: None
	//Returns: Returns your current place with the vector
	int at(int) const;

	//assignment
	//Purpose: Overload the = operator
	//Parameters: The two myVector objects we want to assign
	//Returns: The assignment
	myVector operator=(const myVector&);
};

//Independant Functions

//streamInsertionOverload
//Purpose: Overload the stream insertion operator
//Parameters: The stream object to read from, and object to read to
//Returns:The stream
ostream& operator<<(ostream& out, const myVector& rho);


vectorImplementation.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

#include "vectorHeader.h" 

//Default constructor
//Purpose: Creates a vector
//Parameters: None
//Returns: None
myVector::myVector()
{
	//Setting the cap
	cap = VECTOR_CAP;
	//Creating the array
	vectorData = new int[cap];
	//Initializing the value
	numElements = 0;
}

//Parameterized constructor
//Purpose: Creates a vector capacity of n
//Parameters: None
//Returns: None
myVector::myVector(int n)
{
	//Setting the cap
	cap = n;
	//Creating the array
	vectorData = new int[cap];
	//Initializing the value
	numElements = 0;
}

//Destructor
//Purpose:Deletes any dynamically allocated storage
//Parameters: None
//Returns: None
myVector::~myVector()
{
	cap = 0;
	//Delete array elements
	delete[] vectorData;
	//Allocate vectorData
	vectorData = NULL;
}

//Overloaded assignment operator
//Purpose: To do assignment from on vector to another
//Parameters: A myVector object
//Returns: A myVector object
myVector myVector::operator=(const myVector& rho)
{
	//Test for assingment
	if (this == &rho)
	{
		return *this;
	}

	//Delete lho
	delete[] this->vectorData;

	//Creating new array to fit rho data
	cap = rho.cap;
	this->vectorData = new int[cap];

	//Copying data
	for (int i = 0; i < cap; i++)
	{
		this->vectorData[i] = rho.vectorData[i];
	}

	//Returning myVector object
	return *this;
}

//Size function
//Purpose: returns the size of your vector
//Parameters: None
//Returns: The size of your vector as an integer
int myVector::size() const
{
	//Returning the number of elements
	return numElements;
}

//Capacity function
//Purpose: Returns the capacity of the vector
//Parameters: None
//Returns: Maximum value that your vector can hold
int myVector::capacity() const
{
	//Returning the capacity
	return cap;
}

//Clear function
//Purpose: Deletes all of the elements from the vector and resets its size to zero and its capacity to two; thus becomming empty
//Parameters: None
//Returns: None
void myVector::clear()
{
	//Deleting previous data
	delete[] vectorData;

	//Setting the cap
	cap = VECTOR_CAP;
	//Initializing the value
	numElements = 0;

	// allocate new array 
	vectorData = new int[cap];
}

//push_back function
//Purpose: Adds the integer value n to the end of the vector
//Parameters: Takes a integer to be placed in the vector
//Returns: None
void myVector::push_back(int n)
{
	//If statement to handle if array is full 
	if (numElements == cap)
	{
		//Doubling the capacity 
		cap = cap * VECTOR_CAP;
		//Allocating new array 
		int* newVectorData = new int[cap];
		//Copying data
		for (int i = 0; i < numElements; i++) newVectorData[i] = vectorData[i];
		//Deleting previous data
		delete[] vectorData;
		//Pointing to new data
		vectorData = newVectorData;
	}
	//Storing data
	vectorData[numElements++] = n;
}

//at function
//Purpose: Returns the value of the element at position n in the vector
//Parameters: None
//Returns: Returns your current place with the vector
int myVector::at(int n) const
{
	return vectorData[n];
}

ostream& operator<<(ostream& out, const myVector& rho)
{
	for (int n = 0; n < rho.capacity(); n++)
	{
		out << rho.at(n);
	}
	return out;
}


vectorDriver.cpp
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
48
49
50
51
52
53
54
55
56
57
#include "vectorHeader.h"     
using namespace std;

// the printV function
// used to test the copy constructor
// parameter: a MyVector object
void printV(myVector);

int main()
{
	cout << "\nCreating a vector Sam of size 4.";
	myVector sam(4);

	cout << "\nPush 12 values into the vector.";
	for (int i = 0; i < 12; i++)
		sam.push_back(i);

	cout << "\nHere is sam: ";
	cout << sam;
	cout << "\n---------------\n";

	cout << "\nCreating a vector Joe of size 4.";
	myVector joe(4);
	cout << "\nPush 6 values into the vector.";
	for (int i = 0; i < 6; i++)
		joe.push_back(i * 3);

	cout << "\nHere is joe: ";
	cout << joe;
	cout << "\n---------------\n";

	cout << "\nTest the overloaded assignment operator \"joe = sam\": ";
	joe = sam;

	cout << "\nHere is sam: ";
	cout << sam;
	cout << "\n---------------\n";

	cout << "\nHere is joe: ";
	cout << joe;
	cout << "\n---------------\n";

	// pass a copy of sam by value
	printV(sam);


	cout << endl;
	system("PAUSE");
	return 0;
}

void printV(myVector v)
{
	cout << "\n--------------------\n";
	cout << "Printing a copy of a vector\n";
	cout << v;
}
What exact line(s) are giving you a mistake? You have multiple cout statements after all. :3
So any time that I do output to the console. So on my driver lines 19, 29, 36, 40, 44. Anytime that I try and cout that data to the console.
Does it output correct data followed by a string of random of numbers? You only push_back to numElements, but your insertion overload outputs the maximum capacity in the array.

Let's say you're numElements is 5 but your cap is 10. Your array would have these values:
1, 2, 3, 4, 5, -842150451, -842150451, -842150451, -842150451, -842150451.

You acutally haven't push backed to the last five elements in the array (so they're junk pretty much), but you're insertion operator still outputs them.

Does that make any sense?
That is exactly what it does, I figured they would just be garbage I just couldn't figure out why. That now gives me the question, how do I get rid of those garbage outputs? I am assuming that one of my overloads need to take into account the number of elements in my vector.
I'd personally change your capacity function to return numElements instead. At the very least, I don't see a reason why you would need such ready access to the cap variable anyway outside of member functions.
Okay that makes sense, I changed my variable on line 147 of my implementation code to read back my size function rather than my capacity function and I now have correct output until line 44 on the driver. It prints up to that point and prints a ton of numbers and crashes. Not sure why it is doing that either. To be specific it gets hung up on line 142 of implementation after the call to print.
Last edited on
The line numbers remained the same after you updated your functions? Sounds like a loop error that leads to a crash caused by a segmentation fault/memory error.
The line numbers did remain the same, because I only removed and replaced of call of 'cap' and 'numElements'. I went through the rest of the project and changed my references from 'cap' to 'numElements' and got the correct output. Thanks for your help on this one.
Topic archived. No new replies allowed.