Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 10th, 2008, 6:40 AM   #1
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 79
Rep Power: 1 JD-Salinger is on a distinguished road
_set_new_handler() problem header?

As i try to compile the program, there are some errors in it, stating that redefinition of class bad_alloc() the code is this
c++ Syntax (Toggle Plain Text)
  1. #include <new.h>
  2. #include <iostream>
  3. using std::cout;
  4. using std::cerr;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. class bad_alloc {};
  9.  
  10. int NewHandler (size_t size)
  11. {
  12. throw bad_alloc ();
  13. return 0;
  14. }
  15. int main ()
  16. {
  17. //_set_new_handler (NewHandler);
  18. try
  19. {
  20. status status;
  21. SymbolTable symTab;
  22. Function::Table funTab (symTab);
  23. Store store (symTab);
  24.  
  25. cerr << "\nEnter empty line to quit\n";
  26. do
  27. {
  28. cerr << "> "; // prompt
  29. Scanner scanner (cin);
  30.  
  31. if (!scanner.IsEmpty ())
  32. {
  33. Parser parser (scanner, store, funTab, symTab);
  34. status = parser.Parse ();
  35. if (status == stOk)
  36. {
  37. double result = parser.Calculate ();
  38. cout << result << endl;
  39. }
  40. else
  41. {
  42. cerr << "Syntax error.\n";
  43. }
  44. }
  45. else
  46. {
  47. break;
  48. }
  49. } while (status != stQuit);
  50. }
  51. catch (bad_alloc)
  52. {
  53. cerr << "Out of memory!\n";
  54. }
  55. catch (...)
  56. {
  57. cerr << "Internal error\n";
  58. }
  59. return 0;
  60. }

and then after that i tried to remove the code in line #8 and i tried compiling it again, but another error pops out...saying set_new_handler was not declared in this scope. i tried changing <new.h> to <new> but it still returns same error... does set_new_handler function still exists? cause i just copied the source from a book and the book was way back to 1996.... or I just included a wrong header file? or the function name has changed? thank you so much
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old May 10th, 2008, 9:09 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5 grumpy is on a distinguished road
Re: _set_new_handler() problem header?

I can make an informed guess about the author of the book, but will not reveal that here. The main value of your book is in kilojoules that might be released by burning it.

In any event;

1) There is no header named <new.h> in the C++ standard. However, some pre-standard C++ compilers (produced while the standard was an evolving draft) do support such a header. And, in those headers, bad_alloc was not in namespace std. If you are using one of those old compilers (or some more recent ones that supply such headers for backward compatibility with code written targeting those older compilers) this will explain the ambiguity your compiler is reporting about bad_alloc. Essentially, you have a header declaring a type named bad_alloc and another redeclaring such a type in code. Technically, this is a violation of the One Definition Rule (ODR), so it is actually undefined behaviour. This means, if your compiler library has a header named <new.h> and it declares a bad_alloc type, then your compiler is neither correct nor incorrect in complaining about ambiguity.

2) There is no function named _set_new_hander() in the C++ standard, and never was in any drafts either. There is a function named std::set_new_handler() [ie in namespace std, and without the leading underscore] declared in the standard header <new>, and it is a function that accepts a pointer to a void function with no arguments (not a function accepting a size_t argument, as in your NewHandler() function). If your book is written using _set_new_handler(), then it is a book that was written with a specific compiler in mind ..... ie it is vendor specific.

3) There are several other types (status, SymbolTable, Function::Table, Store, ....) that are completely non-standard. Use of these without any declaration will make any standard compliant compiler complain rather bitterly.
grumpy is offline   Reply With Quote
Old May 10th, 2008, 8:08 PM   #3
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 79
Rep Power: 1 JD-Salinger is on a distinguished road
Re: _set_new_handler() problem header?

The book was really outdated, but the way the author presented the features of c++ is very simple and easy to understand, he presented the features of c++ in real life situations unlike other books which contains bag of tricks,but does not show on how it can be applied in real life programming, I read another book titled "accelerated c++" but the way he presented the information makes me hard to grasp. I am not really good in english so i am having a really hard time understanding other books, but the book i am reading really suits me, so what i do is practicing and coding the information on the book and supplementing it with other reference books.... Anyway, thanks a lot grumpy, you are too cool knowing lots of stuff in programming, I envy you a lot... more power! thank you
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old May 10th, 2008, 9:37 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5 grumpy is on a distinguished road
Re: _set_new_handler() problem header?

I sympathise on the problem of finding easy to read books on C++ for beginners; the most knowledgeable authors tend to write in a style that assumes the reader is interested in concepts rather than practice, and also has a solid grasp of the fundamentals of the language. Unfortunately, information that is simple and easy to understand is worthless if it is wrong and, if it is believed by beginners, does more harm than good.
grumpy is offline   Reply With Quote
Old May 10th, 2008, 9:58 PM   #5
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 79
Rep Power: 1 JD-Salinger is on a distinguished road
Re: _set_new_handler() problem header?

Quote:
Originally Posted by grumpy View Post
I sympathise on the problem of finding easy to read books on C++ for beginners; the most knowledgeable authors tend to write in a style that assumes the reader is interested in concepts rather than practice, and also has a solid grasp of the fundamentals of the language. Unfortunately, information that is simple and easy to understand is worthless if it is wrong and, if it is believed by beginners, does more harm than good.
yah... i agree with that, but in my philosophy C++ is a language, just like the english language, and from what i know and in my experience in learning the "english" language, is from practice and using it day by day. The book tends to introduce the concept and its done in practice by coding it, and i think i can easily adapt to the correct use of the language if i applied it in real life situations and experience and ofcors with your help... heheheh.....
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old May 11th, 2008, 5:46 AM   #6
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,005
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: _set_new_handler() problem header?

If the author of your book is Herbert Schildt, I'd recommend using it only as a paperweight, doorstop, fuel for the fireplace, or page-by-page as cat box liner. Really, he's got that bad of a reputation- so bad, in fact, that the term 'bullschildt' was coined to describe his writings.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old May 11th, 2008, 7:13 AM   #7
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 79
Rep Power: 1 JD-Salinger is on a distinguished road
Re: _set_new_handler() problem header?

Quote:
Originally Posted by lectricpharaoh View Post
If the author of your book is Herbert Schildt, I'd recommend using it only as a paperweight, doorstop, fuel for the fireplace, or page-by-page as cat box liner. Really, he's got that bad of a reputation- so bad, in fact, that the term 'bullschildt' was coined to describe his writings.
Nope... Herbert Schildt is not the author of the book i am reading... but i have one of his books... the C++ Complete Reference Third Edition... I am Reading C++ in Action by Bartosz Milewski... The way he presented the features in c++ is great. . . he presented the features in such a way that youll have an idea on what feature it would be used to... unlike other books which presents concepts and youll be wondering when will you use this feature
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old May 11th, 2008, 7:36 AM   #8
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,005
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: _set_new_handler() problem header?

Well, now you know what to do with that Schildt book.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old May 11th, 2008, 7:55 AM   #9
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 79
Rep Power: 1 JD-Salinger is on a distinguished road
Re: _set_new_handler() problem header?

Quote:
Originally Posted by lectricpharaoh View Post
Well, now you know what to do with that Schildt book.
The Schildt book is just for reference hehehehehe.... do you have any good book to recommend?
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem solving ReggaetonKing Software Design and Algorithms 7 Jan 4th, 2008 1:49 PM
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
send() problem w/ http server kch_86 C 2 Nov 25th, 2005 12:53 AM
question about header files and prototyping functions linuxpimp20 C 13 Sep 7th, 2005 8:28 AM
TCP Header for Echo Communication AusTex C++ 0 Feb 27th, 2005 3:57 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC