-1

I have this vector class, and I was provided with a driver to test the class. Most of it seems to work fine but I think there is something wrong with the exceptions part (which I haven't quite fully understood)

Here is the code for the class .cpp file

int myVector::at(int i)
    {
if(i<vsize)
    return array[i];
    throw 10;
    }

and here is the driver code

#include "myVector.h"
#include <iostream>
using namespace std;

int main()
{
// Create a default vector (cap = 2)
myVector sam;

// push some data into sam
cout << "\nPushing three values into sam";
sam.push_back(21);
sam.push_back(31);
sam.push_back(41);

cout << "\nThe values in sam are: ";

// test for out of bounds condition here
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;
    }
}
cout << "\n--------------\n";

// clear sam and display its size and capacity
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";

// Push 12 values into the vector - it should grow
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...";
// display the values in the vector
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;
}

Any help is appreciated. Thanks

Gama
  • 352
  • 2
  • 16
  • 1
    Why are you trying to implement own vector when there is `std::vector` already? – LihO Oct 13 '13 at 17:25
  • 2
    Where is indentation ? – Arpit Oct 13 '13 at 17:26
  • 1
    Please don't post *all* of your code, only the parts relevant to the problem at hand. In this case the code throwing the exceptions and the code catching them. Also *what* problems with the exceptions do you have? What happens when you throw an exception? What did you expect to happen? – Some programmer dude Oct 13 '13 at 17:27
  • ok,i'll edit it right away, thanks – Gama Oct 13 '13 at 17:29
  • You need to read about [initialization lists](http://stackoverflow.com/q/1711990/140719), _[The Rule of Three](http://stackoverflow.com/q/4172722/140719)_, `const` [member function](http://stackoverflow.com/q/4059932/140719) and a lot more. Also, your `clear()` function violates the class' invariants: `maxsize` != no of allocated `int` objects, `vsize` != no of used ones, `array` pointing to nowhere. (There's more, but that's where I stopped.) I very much hope this homework isn't due tomorrow, because you have a lot of [good reading](http://stackoverflow.com/q/388242/140719) to do. – sbi Oct 13 '13 at 17:40
  • just started the reading, thanks – Gama Oct 13 '13 at 17:50

2 Answers2

1

The driver that you have provided:

try {
    cout << sam.at(i) << " ";
}
catch(int badIndex) {
    cout << "\nOut of bounds at index " << badIndex << endl;
}

expects that int will be thrown (a bit weird design, but well... this is the code that will use your class...). Your implementation of at() might look like this:

int& myVector::at(int i) throw(int) {
    if (i < vsize)
        return array[i];
    throw i;
}

just try to follow one simple rule: throw by value, catch by reference.


Also note that you have a pointer:

private:
    int* array;

which points to dynamically allocated memory allocated in constructor and copy constructor and freed in destructor :

myVector::myVector(int i)
{
    ...
    array = new int[maxsize];
}
myVector::myVector(const myVector& v)//copy constructor 
{
    ...
    array =new int[maxsize];
}
myVector::~myVector()
{
    delete[] array;
}

But how about the assignment operator ? See What is The Rule of Three?

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
0

Your stop condition of for loop ends it one element after the last one (i.e. you cannot access 4th element of sam vector because there are only three elements).

std::vector::at throws std::out_of_range exception in such situation (see: http://en.cppreference.com/w/cpp/container/vector/at), not int one. So you should change your exception handling part to something like this:

#include <exception>

try
{
        cout << sam.at(i) << " ";
}
catch(std::out_of_range exc)
{
    cout << "\nOut of bounds at index " << exc.what() << endl;
}
Michał Góral
  • 1,439
  • 2
  • 16
  • 30