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[i].name<<" "<<phone_book[i].number<<"\n";
}
//--------------------------End of Code----------------