Archived

This topic is now archived and is closed to further replies.

Can anyone help me understand this C++ problem ?

This topic is 5883 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, Below is a code section I have taken from a Book. I have a few questions about it that hopefully will be self explanatory to some of you !. The code is a random Number Generator. // RandInteger.h: interface for the CRandInteger class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_RANDINTEGER_H__AB793E40_CD5D_11D5_8C32_91B0D35AF227__INCLUDED_) #define AFX_RANDINTEGER_H__AB793E40_CD5D_11D5_8C32_91B0D35AF227__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include ////////////////////////////////////////////////////////////////////// // Class Implementation to generate a True Set of Random Numbers // Code Sample Taken From C++ Programming Language, B.Stourstrup // ////////////////////////////////////////////////////////////////////// class CRandInteger { unsigned long randx; public: CRandInteger(long s=0) {randx=s; } void seed(long s) {randx=s; } // magic Numbers chosen to use 31 bits of 32 bit long long abs (long x) { return x&0x7fffffff; } static double max() { return 2147483648.0; }// Max a Double can take long draw() { return randx=randx*1103515245 + 12345; } double fdraw () { return abs(draw())/max(); } // return a number in interval 0 , 1 long operator()(){ return abs(draw()); } // return number in interval 0, 10*2^31 }; #endif // !defined(AFX_RANDINTEGER_H__AB793E40_CD5D_11D5_8C32_91B0D35AF227__INCLUDED_) When I compile this code, I get a whole host of errors to do with the max macro, so I suspect I am using the wrong include statement ? I also get a problem with the long draw() function. And for a 3rd question, what does the long operator()() actually do (I mean in general, not return the absolute of whatever draw throws up !) Look forwards for some explanation !

Share this post


Link to post
Share on other sites
1. Max() isn''t a macro, it''s a static method of the class CRandInteger, to call it you need to call CRandInteger::max()
2. Can you give me more information as to what goes wrong with draw?
3. ????? It looks like an operator overload statement - sort of!

Hope that helps a little

Share this post


Link to post
Share on other sites
1. The max macro takes two arguments. You are using it with none, which is probably why it isnt working.
[edit]
Duh. I didnt see that. Use the static function as above.

2. Dunno. What are the errors it is spewing out?

3. It overloads the () operator. eg

    
// without overload


CRandInteger random;
random.draw();

// with overload


CRandInteger random;
random();


Edited by - Sandman on November 1, 2001 8:19:01 AM

Share this post


Link to post
Share on other sites
I Agree the code sample is messy, it was lifted straight out of a book. (as mentioned in the comments)

I did not tidy it up as I was just interested in how it worked, being a relative newbie to C++.
Thanks to those of you who pointed me in the right direction. As this was all inline functions in the .h file I didn''t need the CRandInteger:: prefix to max, (when I added it it made no difference) but changing the name to RandMax worked, so I guess that max is a key word and shouldn''t be used. (In which case, I have to distrust other code examples from same book, which is annoying as it was written by the creator of C++)

What does the operator() () signify ? Does that mean that any sub classes called use the parent class''s functions ?

Thanks for the help so far.

Bp.

Share this post


Link to post
Share on other sites