Range-Checked Vector Problem

Discussion in 'C++' started by Steven Spear, Nov 1, 2004.

  1. Steven Spear

    Steven Spear Guest

    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----------------
    Steven Spear, Nov 1, 2004
    #1
    1. Advertising

  2. Steven Spear

    Mike Wahler Guest

    "Steven Spear" <> wrote in message
    news:...
    > 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
    Mike Wahler, Nov 1, 2004
    #2
    1. Advertising

  3. Steven Spear

    Smitsky Guest

    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
    Smitsky, Nov 1, 2004
    #3
  4. >
    > 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
    Thierry Miceli, Nov 1, 2004
    #4
  5. "Smitsky" <> wrote in message
    news:EHhhd.14307$...
    > 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
    John Harrison, Nov 1, 2004
    #5
  6. Steven Spear

    Mike Wahler Guest

    "Thierry Miceli" <> wrote in message
    news:AFhhd.11510$...
    > >
    > > 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
    Mike Wahler, Nov 1, 2004
    #6
  7. Steven Spear

    Steven Spear Guest

    > 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
    Steven Spear, Nov 1, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yurps
    Replies:
    1
    Views:
    6,860
    rajeshthangarasu1983
    Feb 29, 2008
  2. pmatos
    Replies:
    6
    Views:
    23,722
  3. Replies:
    8
    Views:
    1,888
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    541
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    343
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page