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.
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>
usingnamespace std;
//Declaring constant
constint 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 = newint[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);
#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 = newint[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 = newint[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 = newint[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 = newint[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 = newint[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;
}
#include "vectorHeader.h"
usingnamespace 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;
}
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.
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.
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.