Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Range-Checked Vector Problem

Reply
Thread Tools

Range-Checked Vector Problem

 
 
Steven Spear
Guest
Posts: n/a
 
      11-01-2004
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----------------
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      11-01-2004

"Steven Spear" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
Smitsky
Guest
Posts: n/a
 
      11-01-2004
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


 
Reply With Quote
 
Thierry Miceli
Guest
Posts: n/a
 
      11-01-2004
>
> Vec<entry> phone_book(3);
>

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);

Cheers,

Thierry
www.ideat-solutions.com

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-01-2004

"Smitsky" <(E-Mail Removed)> wrote in message
news:EHhhd.14307$(E-Mail Removed) hlink.net...
> 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


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      11-01-2004

"Thierry Miceli" <(E-Mail Removed)> wrote in message
news:AFhhd.11510$(E-Mail Removed). ..
> >
> > Vec<entry> phone_book(3);
> >

> 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


 
Reply With Quote
 
Steven Spear
Guest
Posts: n/a
 
      11-01-2004
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
STL algorithm problem vector<vector<double> > and find Dennis C++ 1 06-07-2004 10:09 PM



Advertisments