-
June 11th, 2012, 03:17 PM
#1
Serious lack of OOP understanding. Dependencies in classes.
Hi
i have two classes. CHand1326 and CHand169. In both of them i make use of an object of each other. But somehow this produces following error:
Code:
1>c: ...\Hand1326.h(23): error C2061: Syntaxfehler: Bezeichner 'CHand169'
1>c: ...\Hand169.h(22): error C2065: 'CHand1326': nichtdeklarierter Bezeichner
meaning syntax error with CHand169 an that CHand1326 is not declared. I tried a lot of stuff ... reducing or removing consts added default constructor even assingment operator and copyconstructor. But nothing helps. Please help me! I guess I can't see the forest for the trees...
Specific hand:
Code:
#ifndef CHAND1326_H
#define CHAND1326_H
#include <stdlib.h>
#include <Windows.h>
#include "poker_defs.h"
#include "Hand169.h"
class CHand1326{
public:
CHand1326(){};
CHand1326(const char Rank1, const char Rank2, const char Suit1, const char Suit2);
bool operator< (CHand1326 rPar) const;
bool operator> (CHand1326 rPar) const;
bool operator<=(CHand1326 rPar) const;
bool operator>=(CHand1326 rPar) const;
bool operator==(CHand1326 rPar) const;
bool isSuited() const { return mSuit1 == mSuit2;};
bool isPair() const { return mRank1 == mRank2;};
CardMask GetCardMask() const;
void To169(CHand169 *ret) const;
char mRank1, mRank2, mSuit1, mSuit2;
};
#endif // CHAND1326_H
Handcategory
Code:
#ifndef __CHAND169_H
#define __CHAND169_H
#include <stdlib.h>
#include <Windows.h>
#include <set>
using std::set;
#include "Hand1326.h"
class CHand169{
public:
CHand169(){};
CHand169(const char Rank1, const char Rank2, const bool isSuited);
const bool operator< (CHand169 rPar) const;
const bool operator> (CHand169 rPar) const;
const bool operator<= (CHand169 rPar) const;
const bool operator>= (CHand169 rPar) const;
const bool operator== (CHand169 rPar) const;
const bool isPair ()const {return mRank1 == mRank2;}
const int ToCHand1326 (set<CHand1326 >& rHands1326) const;
char mRank1, mRank2;
bool isSuited;
};
#endif // __CHAND169_H
Last edited by ImNotaBot; June 11th, 2012 at 03:18 PM.
Reason: Forgot title
-
June 11th, 2012, 04:13 PM
#2
Re: Serious lack of OOP understanding. Dependencies in classes.
Originally Posted by ImNotaBot
Hi
i have two classes. CHand1326 and CHand169. In both of them i make use of an object of each other. But somehow this produces following error:
Code:
1>c: ...\Hand1326.h(23): error C2061: Syntaxfehler: Bezeichner 'CHand169'
1>c: ...\Hand169.h(22): error C2065: 'CHand1326': nichtdeklarierter Bezeichner
meaning syntax error with CHand169 an that CHand1326 is not declared. I tried a lot of stuff ... reducing or removing consts added default constructor even assingment operator and copyconstructor. But nothing helps. Please help me! I guess I can't see the forest for the trees...
Look at your header files closely.
This is in Hand1326.h
Code:
#include "Hand169.h"
Then this is in Handl169.h
Code:
#include "Hand1326.h"
This is an infinite recursive inclusion and is not going to work. Hand1326.h includes Hand169.h, which includes Hand1326, which includes Hand169.h, which etc...
1) You should forward declare your classes instead of including the entire header file.
2) Pass classes by reference or const reference, not by value:
Code:
#ifndef CHAND1326_H
#define CHAND1326_H
#include <stdlib.h>
#include <Windows.h>
#include "poker_defs.h"
class CHand169;
class CHand1326{
public:
CHand1326(){};
CHand1326(const char Rank1, const char Rank2, const char Suit1, const char Suit2);
bool operator< (const CHand1326& rPar) const;
bool operator> (const CHand1326& rPar) const;
bool operator<=(const CHand1326& rPar) const;
bool operator>=(const CHand1326& rPar) const;
bool operator==(const CHand1326& rPar) const;
bool isSuited() const { return mSuit1 == mSuit2;};
bool isPair() const { return mRank1 == mRank2;};
CardMask GetCardMask() const;
void To169(CHand169 *ret) const;
char mRank1, mRank2, mSuit1, mSuit2;
};
#endif // CHAND1326_H
Now there is no #inclusion of Hand169 and also note the parameters to those operator functions (in reality, you really only need to write operator < and operator ==, then use std::rel_ops or boost operators to fill in the rest.
See here:
http://www.cplusplus.com/reference/std/utility/rel_ops/
Only two operators need to be written, and the rel_ops template creates the other operations in terms of < and ==. The boost version is a little more powerful, but I haven't used it.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; June 11th, 2012 at 04:16 PM.
-
June 11th, 2012, 05:21 PM
#3
Re: Serious lack of OOP understanding. Dependencies in classes.
Mr. McKenzie, you are great!
Thanks for the rel_ops tip.
I already knew the forward declaration, but I will have to study it even more exactly. Sometimes it just does not come into my mind to use it.
But what if I want to, or even have to, make use of valuetypes? E.g. in case of returning the type.
Code:
//void To169(CHand169 *ret) const;
CHand169 To169() const;
I know i could use pointers here too, but then i have to take care of memorymanagement precisely. This is something I really want to avoid. I once had to fix a memoryleak in a larger project. I took me a few days...
-
June 12th, 2012, 01:17 AM
#4
Re: Serious lack of OOP understanding. Dependencies in classes.
You could return a smart pointer instead of a raw pointer. See for instance http://en.cppreference.com/w/cpp/memory/shared_ptr or http://www.boost.org/doc/libs/1_49_0...shared_ptr.htm depending on the compiler you use.
-
June 12th, 2012, 05:07 AM
#5
Re: Serious lack of OOP understanding. Dependencies in classes.
Originally Posted by ImNotaBot
But what if I want to, or even have to, make use of valuetypes? E.g. in case of returning the type.
Code:
//void To169(CHand169 *ret) const;
CHand169 To169() const;
Then you should first reconsider your design. In your case, it seems like the conversion functions could easily be defined outside of the class scope, such that they can be in a separate file that includes the header files for both classes. The two classes then don't have to know anything about each other.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|