I am working on a vector class to fill it with data. When I run the code it outputs the correct data in the void myVector::push_back(int valueIn) function but int the print function it out puts garbage values. I thought the arr pointer, which is a dynamic array, pointed to the same data in both functions. Any help would be appreciated.
Thanks
#include "myVector.h"
#include <iostream>
usingnamespace std;
myVector::myVector()
{
arr = newint[ 10 ];
size = 0;
capacity = 10;
}
myVector::~myVector()
{
if (capacity >0)
{
delete []arr;
}
capacity = 0;
size = 0;
}
// main is sending the valueIn data
void myVector::push_back(int valueIn)
{
arr = &valueIn;
cout << *arr << " "; // I used this cout to check if I had the right
} // values, of which I do
void myVector::print()
{
for( int i = 0; i < capacity; i++ )
{
cout << arr << " ";
}
}
Your push back assigns the buffer to a pointer to a temporary value.
It should copy the buffer in a larger array, insert the argument as last element of that array and delete the old buffer.
I was trying to load the arr with the values sent in from valueIn. Evidently that is in correct.
I tried some other stuff but now my print function only prints out the number 18 "capacity" times which is the last number coming in from valueIn. valueIn is 2,4,6,8,10,12,14,16,18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void myVector::push_back(int valueIn)
{
for( int i = 0; i < capacity; i++ )
arr[ i ] = valueIn;
cout << *arr << " "; // I used this cout to check if I had the right
} // values, of which I do
void myVector::print()
{
for( int i = 0; i < capacity; i++ )
cout << arr[ i ] << " ";
}
// output to the screen is 18 18 18 18 etc.
Bazzy I am not sure what your talking about as far as the buffer. Could you give an explanation or example?
Thanks for the reply guestgulkan.
You're assumptions are correct about the size and capacity. I will play with it some more and see what else I can do.
I just did not understand how it would print out the correct values in the push_back() and the same 18 value in the print() but I think I got it know.
Thanks a lot. I am new at this stuff and some people treat newbies like they weren't newbies themselves at one point, so I appreciate the help.
I have played with this thing and man am I having fun. I got it to print the right output but when I run it I get an error saying the heap is corrupted. I think I need to use delete somewhere but I don't know where. Any ideas?
Thanks a lot fun2code. I think I m getting this stuff. I have an erase function that I am struggling with. I think there is one thing I am missing but I can't get.
void myVector::erase(int location)
{
for( int i = 0; i < size; i++ )
{
if( arr[ i ] == location )
{
int* temp = newint[ capacity-- ];
size--;
// I think something has to go here to increment the pointer. Every time I
// run the code it only deletes the last number in the array and not the location
// sent into the function
for( int i = 0; i < size; i++ )
temp[ i ] = arr[ i ];
delete [] arr;
arr = temp;
}
}
}
Example: The array prints out 2 4 6 8 10 12 14 16 18.
I need to delete the number 4, or 6, or etc, ( this is int location) but it only deletes the 18.
Any thoughts?
Thanks again.
Yes I have thoughts about it. I'm a bit confused. I'm going to assume that by "location" you mean "value". eg. the value stored in the 3rd element ( a location ) is 6. It seems fairly clear that this is what you mean.
If you wish to erase a value from the array simply copy all of the following values back one space.
There is no need to delete or new anything. The existing array can stay the same size. You even regain some excess capacity!
Since a given value may or may not exist in the array (eg. 22 does not so erase(22) would fail) the function should return a bool result to indicate success or failure.
Try this and let me know if it does what you need here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
bool myVector::erase(int value )
{
int i=0;// start search at beginning of array
while( arr[i] != value )// find index where value is stored.
{
++i;
if( i == size )// entire array has been gone through
returnfalse;// value was not found
}
// value was found at element i - erase it
for(int j=i; j<size-1; j++)
arr[j] = arr[j+1];// copy back one space
size--;// reduce size by one
returntrue;
}
EDIT: If you don't follow the logic, please ask. I'm trying to help you understand this.
Thanks a lot fun2code. I think I understand it and it works. I learned more from your 29 lines of code than the hours of reading books and watching you tube. I really appreciate your time.
I noticed that you used the pre-increment operator in the while loop and a post- increment in the for loop is there any reason in particular as to why you did that?
firedraco is correct. The switching back and forth is due solely to inconsistent practice on my part.
I shall try to tighten that up (and use pre-increment where post-increment isn't needed).