473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Range-Checked Vector Problem

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----------------
Jul 22 '05 #1
6 1510

"Steven Spear" <sm*****@hotmail.com> wrote in message
news:66**************************@posting.google.c 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
Jul 22 '05 #2
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
Jul 22 '05 #3
>
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

Jul 22 '05 #4

"Smitsky" <sm*****@mindspring.com> wrote in message
news:EH*******************@newsread2.news.atl.eart 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
Jul 22 '05 #5

"Thierry Miceli" <no****@nospam.com> wrote in message
news:AF********************@news20.bellglobal.com. ..

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
Jul 22 '05 #6
> 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
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Xah Lee | last post by:
Today we'll be writing a function called Range. The Perl documentation is as follows. Perl & Python & Java Solutions will be posted in 48 hours. This is Perl-Python a-day. See...
4
by: Bill R via AccessMonster.com | last post by:
I get this Run-Time Error 1004 whenevery the following code runs: On Error GoTo XLSheet2 Set objXL = CreateObject("Excel.Application") With objXL Set objWkb = .Workbooks.Open(strPath) With...
2
by: George | last post by:
Is there a fast way to transfer an Excel range to an array? Example: Excel range is E2:E300 Dim person() as string Thanks, George
29
by: Steve R. Hastings | last post by:
When you compile the expression for i in range(1000): pass does Python make an iterator for range(), and then generate the values on the fly? Or does Python actually allocate the list and...
45
by: Summercoolness | last post by:
it seems that range() can be really slow: the following program will run, and the last line shows how long it ran for: import time startTime = time.time() a = 1.0
17
by: stdazi | last post by:
Hello! Many times I was suggested to use xrange and range instead of the while constructs, and indeed, they are quite more elegant - but, after calculating the overhead (and losen flexibility)...
20
by: Junmin H. | last post by:
Hello, I am trying to print the range of unsigned char and unsigned int. The one for char is working good, gives me a correct output, however the other one for int doesnt, why?? Thanks #include...
2
by: Joe Goldthwaite | last post by:
I've been playing with Python a bit. Doing little performance benchmarks and working with Psyco. It's been fun and I've been learning a lot. For example, in a previous post, I was looking for a...
0
by: iain654 | last post by:
I have finally worked out how to automatically send a range of cells in the body of an email using Outlook but it is very clumsy and I have to build up the email using the limit of line...
0
by: drfish | last post by:
Hi, I'm a complete novice when it comes to VB so need some help. I would like a macro that performs linear regression for different cell ranges each time it is run, depending on the number of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.