Archived

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

Can anyone help me understand this C++ problem ?

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

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 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 on other sites
1. The max macro takes two arguments. You are using it with none, which is probably why it isnt working.

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 overloadCRandInteger random;random.draw();// with overloadCRandInteger random;random();

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

Share on other sites
check out 9-th line of your code!

Share on other sites
I can''t comment on the actual problem, but your code is "in ym opinion" a little messy

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.

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 19
• 10
• 14
• 10
• Forum Statistics

• Total Topics
632664
• Total Posts
3007708
• Who's Online (See full list)

There are no registered users currently online

×