473,769 Members | 6,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

won't compile, parse error?

I am getting a parse error from g++ pointing at my catch line and can't
figure out whats wrong with this code:

#include "BigPosInt. h"
#include <iostream>
#include <new>
#include <assert.h>

BigPosInt::BigP osInt(int init_max_digits )
{
assert(init_max _digits > 0);

try
{
digitsArray = new int[init_max_digits];
}

catch(bad_alloc a)
{
const char* temp = a.what();
std::cout<<"\n\ n"<<temp<<"\ n";
std::cout<<"Ins ufficient free memory. \n\n";
}

// Postcondition: constructed BigPosInt has been initialized to
// represent the value 0 and can accommodate any
// BigPosInt that has up to init_max_digits digits
}

Any ideas?
thanx,
Christopher
Jul 19 '05 #1
19 3784
On Wed, 24 Sep 2003 04:17:17 GMT, "Christophe r" <cp***@austin.r r.com> wrote:
I am getting a parse error from g++ pointing at my catch line and can't
figure out whats wrong with this code:

#include "BigPosInt. h"
#include <iostream>
#include <new>
#include <assert.h>
Just to get into the habit try using <cassert> instead of <assert.h>.
Since 'assert' is a macro it's practically the same. But not for
other old C library headers.
BigPosInt::Big PosInt(int init_max_digits )
Here I would have used unsigned type for 'init_max_digit s'. However,
about 50% (as far as I can determine) of the community disagrees.
The 50% or so who agree think that 'unsigned' communicates the
"must be non-negative" requirement more clearly.

{
assert(init_max _digits > 0);
For 'unsigned' you'd want to make this 'assert( init_max_digits <= INT_MAX )',
just to be on the safe side.

try
{
digitsArray = new int[init_max_digits];
Assuming 'digitsArray' is a member variable.
}

catch(bad_alloc a)
Make that 'catch( std::bad_alloc const& a )'

{
const char* temp = a.what();
You don't need a temp variable here.

std::cout<<"\n\ n"<<temp<<"\ n";
std::cout<<"Ins ufficient free memory. \n\n";
Remember to rethrow the exception!

You'd probably also be better off flushing std::cout here.
std::cout << std::flush;
throw;
}

// Postcondition: constructed BigPosInt has been initialized to
// represent the value 0 and can accommodate any
// BigPosInt that has up to init_max_digits digits
If you don't rethrow the exception you will violate that
postcondition in case of an exception.
}


Jul 19 '05 #2
In article <hV************ ********@twiste r.austin.rr.com >, Christopher wrote:
I am getting a parse error from g++ pointing at my catch line and can't
figure out whats wrong with this code: I'm not one to be nit-picky but you might want to past the complete
diagnostic error next time.
#include "BigPosInt. h"
#include <iostream>
#include <new>
#include <assert.h>

BigPosInt::BigP osInt(int init_max_digits )
{
assert(init_max _digits > 0);

try
{
digitsArray = new int[init_max_digits];
}

catch(bad_alloc a) catch(std::bad_ alloc a)
// I see no using namespace std::bad_alloc
// or using namespace std; etc..
{
const char* temp = a.what();
std::cout<<"\n\ n"<<temp<<"\ n";
std::cout<<"Ins ufficient free memory. \n\n";
}

// Postcondition: constructed BigPosInt has been initialized to
// represent the value 0 and can accommodate any
// BigPosInt that has up to init_max_digits digits
}

Any ideas?
There's one for ya
thanx,
Christopher

Hope that helps,
Chris Johnson
--
echo "qr*********@rk pvgr.pbz" | rot13
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 19 '05 #3
Alf P. Steinbach wrote:
[SNIP]
BigPosInt::BigP osInt(int init_max_digits )
Here I would have used unsigned type for 'init_max_digit s'. However,
about 50% (as far as I can determine) of the community disagrees.
The 50% or so who agree think that 'unsigned' communicates the
"must be non-negative" requirement more clearly.


Most of the platforms don't agree with you. They cannot allocate or address
more bytes than the maximum of std::ptrdiff_t, and std::ptrdiff_t is signed.
std::cout<<"\n\ n"<<temp<<"\ n";
std::cout<<"Ins ufficient free memory. \n\n";


Remember to rethrow the exception!

You'd probably also be better off flushing std::cout here.


Why not simply use endl; above? Or better off cerr?
std::cout << std::flush;
throw;


Rethrowing is a good point.

--
Attila aka WW
Jul 19 '05 #4
Christopher wrote:
I am getting a parse error from g++ pointing at my catch line and
can't figure out whats wrong with this code:

#include "BigPosInt. h"
#include <iostream>
#include <new>
#include <assert.h>

BigPosInt::BigP osInt(int init_max_digits )
{
assert(init_max _digits > 0);

try
{
digitsArray = new int[init_max_digits];
}

catch(bad_alloc a)


catch(std::bad_ alloc const &a)

--
Attila aka WW
Jul 19 '05 #5
In article <sl************ *********@gesta lt.localdomain> , Chris Johnson wrote:
In article <hV************ ********@twiste r.austin.rr.com >, Christopher wrote:
catch(std::bad_ alloc a)
// I see no using namespace std::bad_alloc
// or using namespace std; etc..

While I don't want to tear your code snippet apart
(you did ask a very specific question)
I want to correct something in my reply:

catch(std::bad_ alloc const& a)

I think in the end run this is more what you will be after
although my original post 'should' remove your parse error

Other points of mention are things like
#include <cassert>
instead of
#include <assert.h>
as well as other things - I'm not trying to "beat you up" though
I am sure more posters will correct some other parts of your
code, none of it with malicious intent of course.

Chris Johnson
--
echo "qr*********@rk pvgr.pbz" | rot13
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 19 '05 #6
In article <bk**********@n ewstree.wise.ed t.ericsson.se>, Attila Feher wrote:
Christopher wrote:

catch(std::bad_ alloc const &a)

--
Attila aka WW


Yes yes - I noticed that immediately after I sent it and I am kicking
myself over that one. Unfortunately I couldn't delete the post before
it went out. I got in a hurry and didn't catch I was not finished with
my editing. Oh well nothing like leaving my mark in history on USENET
that I sometimes toggle the idiot bit and leave a public record of it.

D'OH!

Chris Johnson
--
echo "qr*********@rk pvgr.pbz" | rot13
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 19 '05 #7
On Wed, 24 Sep 2003 07:49:33 +0300, "Attila Feher" <at**********@l mf.ericsson.se> wrote:
Alf P. Steinbach wrote:
[SNIP]
BigPosInt::BigP osInt(int init_max_digits )
Here I would have used unsigned type for 'init_max_digit s'. However,
about 50% (as far as I can determine) of the community disagrees.
The 50% or so who agree think that 'unsigned' communicates the
"must be non-negative" requirement more clearly.


Most of the platforms don't agree with you. They cannot allocate or address
more bytes than the maximum of std::ptrdiff_t, and std::ptrdiff_t is signed.


Allocation error throws, too large or small range is not an issue here.

std::cout<<"\n\ n"<<temp<<"\ n";
std::cout<<"Ins ufficient free memory. \n\n";


Remember to rethrow the exception!

You'd probably also be better off flushing std::cout here.


Why not simply use endl; above? Or better off cerr?


Right. Even better, not do debug i/o at that level. I considered
how much to write about it, and I think I chose a good level to
further progress. Otherwise there's even more to say.
std::cout << std::flush;
throw;


Rethrowing is a good point.


All my points are good... ;-)

Jul 19 '05 #8
Alf P. Steinbach wrote:
On Wed, 24 Sep 2003 07:49:33 +0300, "Attila Feher"
<at**********@l mf.ericsson.se> wrote:
Alf P. Steinbach wrote:
[SNIP]
BigPosInt::BigP osInt(int init_max_digits )

Here I would have used unsigned type for 'init_max_digit s'.
However, about 50% (as far as I can determine) of the community
disagrees. The 50% or so who agree think that 'unsigned'
communicates the "must be non-negative" requirement more clearly.
Most of the platforms don't agree with you. They cannot allocate or
address more bytes than the maximum of std::ptrdiff_t, and
std::ptrdiff_t is signed.


Allocation error throws, too large or small range is not an issue
here.


IMHO it is. If you give an interface saying: I can do 32 and in practice
portably you can only do 16 (and portably mean eg: Windows cannot) this is
not OK. Also according to Bjarne just to rule out negative numbers you must
not use unsigned. According to Dan Saks using std::size_t will lead to
problems, since most implementations will not support it.

And for you short, IMHO arrogant reply about it being a non-issue: if you
prefer runtime errors over compile time errors (khm: warnings) then I think
there is not much to discuss.
std::cout<<"\n\ n"<<temp<<"\ n";
std::cout<<"Ins ufficient free memory. \n\n";

Remember to rethrow the exception!

You'd probably also be better off flushing std::cout here.


Why not simply use endl; above? Or better off cerr?


Right. Even better, not do debug i/o at that level.


I see. So you prefer having a high level debug message saying "Something
went wrong somewhere"?
I considered how much to write about it,
and I think I chose a good level to
further progress. Otherwise there's even more to say.


Without knowing anything about the intent of the program snippet and its
context? What would those be?
std::cout << std::flush;
throw;


Rethrowing is a good point.


All my points are good... ;-)


Except of the unsigned. It makes the interface communicate a lie on most
platforms and in addition to this moves a compile time detectable bug to
runtime.

--
Attila aka WW
Jul 19 '05 #9
Chris Johnson wrote:
[SNIP]
my editing. Oh well nothing like leaving my mark in history on USENET
that I sometimes toggle the idiot bit and leave a public record of it.


Been there, done that. More times than I care to admit. But this is the
way we learn. We make mistakes. At least mine does not start a war or a
blackout of a country. :-)

--
Attila aka WW
Jul 19 '05 #10

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

Similar topics

4
2232
by: Danny Boelens | last post by:
Hi all, today I ran into a compile error after a compiler upgrade. I made a small example to demonstrate my compile error: template<typename T1, typename T2> class A {}; class B
8
16848
by: pavel.orehov | last post by:
Hi, I am using flex and bizon to write HTTP parser. I am passing well flex and bison tools but can't compile their output. ================= Flex file (http_parser.lpp) ============== %{ #include <iostream> #include "http_parser.tab.hpp"
1
2040
by: Slavisa | last post by:
When I make my program on RedHat 9.0 everything goes fine, but when I try to make on Red Hat 7.2 I get following errors: logic2_msgcln.c:178: warning: initialization makes integer from pointer without a cast logic2_msgcln.c:178: initializer element is not constant logic2_msgcln.c:178: warning: data definition has no type or storage class logic2_msgcln.c:185: parse error before `void' logic2_msgcln.c:225: parse error before `('
10
19729
by: Chris LaJoie | last post by:
Our company has been developing a program in C# for some time now, and we haven't had any problems with it, but just last night something cropped up that has me, and everyone else, stumped. I have a struct that contains several different types of data. This struct is used throuout the program. Now, when I compile, I get 6 errors, all of them "Use of possibly unassigned field 'awayTime'" or "Use of possibly unassigned field 'intlTime'"....
3
1191
by: billr | last post by:
Excuse me if you consider this a cross post, however, it was originally destined for this group not the vc.atl group, but somehow it went there instead of here! (obviously it was something I did, but hey ho) ... now back to the problem in hand I've made what I consider to be the simplest of applications, but it won't work. This is my code #using <mscorlib.dll> #include <tchar.h> using namespace System; int _tmain(void) {
2
3745
by: Lonewolf | last post by:
hi all, I realize the example on MSDN for IPCChannel has compile error in VS2005 pro. Either I'm missing something or there's something seriously wrong with MSDN on that section. I reproduce the MSDN code sample for IPCchannel for server side in c# below. please help me see what I'm missing here. using System; using System.Runtime.Remoting.Channels.Ipc;
3
7582
by: sachinvyas | last post by:
Hi, I have following schema saved in new.xsd <?xml version="1.0" encoding="utf-8"?> <xs:schema targetNamespace="http://www.smpte-ra.org/schemas/429.7/2006/CPL" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:cpl="http://www.smpte-ra.org/schemas/429.7/2006/CPL"
16
2704
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public System.Collections.Generic.List<frmMain.sDBTest> LoadTestSet(string TestSetFile, System.Collections.Generic.List<frmMain.sDBTestDBviewList)
2
6660
myusernotyours
by: myusernotyours | last post by:
Hi All, Am working on a Java application in which I have to use the JNI to Interface with some native code for both windows and unix. Am using netbeans IDE with the C/C++ pack installed. Am also using Cygwin as my compiler (gcc), this is ostensibly because I hope this compiler will also compile the unix native libraries since I don't have a Linux installation. (I am working on a personal project from the office and can't get linux installed)....
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.