Range-Checked Vector Problem

S

Steven Spear

Hi. Can you please explain to me why this doesn't work? It gives a
compile-time error at "out_of_range" in Dev-C++. Thanks.

//---------------Code appears here:------------------

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
using namespace std;

template<class T> class Vec : public vector<T> {
public:
Vec() : vector<T>() {}
Vec(int s) : vector<T>(s) {}

T& operator[] (int i) { return at(i); }
const T& operator[] (int i) const { return at(i); }
};

void print_entry(int i);
void f();

struct entry
{
string name;
int number;
};

Vec<entry> phone_book(3);

int main()
{
phone_book[0].name="Smitty Spear";
phone_book[0].number=5;
phone_book[1].name="Debbie Engesser";
phone_book[1].number=7;
phone_book[2].name="David Lasky";
phone_book[2].number=9;
phone_book[3].name="Stevie Moskovitz";
phone_book[3].number=20;
f();
system("pause");
return 0;
}

void f()
{
try {
for(int i=0; i<4; i++) print_entry(i);
}
catch (out_of_range){
cout<<"Range error\n";
}
}

void print_entry(int i)
{
cout<<phone_book.name<<" "<<phone_book.number<<"\n";
}
//--------------------------End of Code----------------
 
M

Mike Wahler

Steven Spear said:
Hi. Can you please explain to me why this doesn't work? It gives a
compile-time error at "out_of_range" in Dev-C++. Thanks.

//---------------Code appears here:------------------

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>

#include <stdexcept>

-Mike
 
S

Smitsky

Thanks Mike. Now I get the console to appear and disappear without being
able to see my warning. Is there anything I can do (I have tried
system("PAUSE"), and cin.get()) to view the message? Thanks. Steve
 
J

John Harrison

Smitsky said:
Thanks Mike. Now I get the console to appear and disappear without being
able to see my warning. Is there anything I can do (I have tried
system("PAUSE"), and cin.get()) to view the message? Thanks. Steve

If you want to catch an exception you need to put a try catch block around
the code that is causing the exception. You have an exception before you
ever get to your try catch block in f. Try this

int main()
{
try
{
phone_book[0].name="Smitty Spear";
phone_book[0].number=5;
phone_book[1].name="Debbie Engesser";
phone_book[1].number=7;
phone_book[2].name="David Lasky";
phone_book[2].number=9;

// exception thrown HERE
phone_book[3].name="Stevie Moskovitz";
phone_book[3].number=20;
}
catch (out_of_range)
{
...
}
cin.get();
}

john
 
M

Mike Wahler

Thierry Miceli said:
Here your reserve only three entries for your vector while you are indexing
entries 0 to 3 (4 entries) in your code.
The above line should be:

Vec<entry> phone_book(4);

You're missing the point. Steven is intentionally
accessing out of bounds in order to produce an
exception (I suppose he's testing).

-Mike
 
S

Steven Spear

You're missing the point. Steven is intentionally
accessing out of bounds in order to produce an
exception (I suppose he's testing).

-Mike

Thanks Mike and everyone. Yes, I am testing code I learned in The C++
Programming Language by Stroustrup. The code is a version of something
he has elaborated on. Without knowing to "#include<stdexcept>," I was
getting nowhere fast! Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top