Declaring A Subclass, Subtyping

Hello, thanks for any help ahead of time, I have a player class and I am trying to subtype it with a Simple player. I can't use any other approach, and can only change main. But when I get the pointer to the simple player, it doesn't recognize the subtype.

I have:

1
2
3
4
5
6
7
8
9
10
#include "player.h"


class Simple : public Player {
	public:
		int bet(unsigned int bankroll, unsigned int minimum);
		bool draw(Card dealer, const Hand &player);
		void expose(Card c){}
		void shuffled(){}
};


with definitions, then I have:

1
2
3
4
5
static Simple simple;

Player *get_Simple(){
	return &simple;
}


and in my main I try to:
1
2
Player * s = get_Simple();
s.bet(5,10);


but I get:

main.cpp: In function `int main()':
main.cpp:14: error: request for member `bet' in `s', which is of non-class type
`Player*'

also, why when I do:
Simple s;

I get

main.cpp:12: error: `Simple' was not declared in this scope
main.cpp:12: error: `s' was not declared in this scope

*s.bet(5,10);

gives:

main.cpp: In function `int main()':
main.cpp:14: error: request for member `bet' in `s', which is of non-class type
`Player*'

But at this point I am just trying things )

Thank you!
when dealing with pointers (assume Player is a class/struct).

Player* p;

To access a member of class Player through pointer p - then it is:
p->member NOT p.member


*p. member would give an error because operator precedence
means that this is the same as *(p.member) and as we said a few lines above that using the dot operator on a pointer to access a member is an error.
In this case it should be (*p).member
Last edited on
You don't show the Player class so I can't be sure, but it seems to me that bet() is a member function of Simple, not Player. Therefore, you cannot call for bet() from a pointer of type Player. Also, I think you should do (*s).bet(5, 10); because of operator priority. But of course, that won't work either because s is of type Player*.
You cannot go backwards in the inheritance tree (sub-type is not the right technical word). Consider the following code and read its comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Base
{
	public:
	void fnBase (void)
	{}
};

class Derived : public Base
{
	public:
	void fnDerived (void)
	{}
};

int main (void)
{
	Base *objBase;
	Derived *objDerived;

	objBase = objDerived = new Derived;

	/*************************************
	Use -> operator
	to call the functions.
	**************************************/

	objDerived->fnBase ();
	/*************************************
	Allowed.
	Derived inherits everything from Base.

	*objDerived.fnBase () can also be used.
	**************************************/

	objBase->fnDerived ();
	/*************************************
	Not allowed.
	Function fnDerived is not known to Base.
	**************************************/
}


If you want to call one of the function of the derived class from its base class, use virtual functions. See the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Base
{
	public:
	virtual void fnVirtual (void)
	{}
};

class Derived : public Base
{
	public:
	virtual void fnVirtual (void)
	{}
};

int main (void)
{
	Base *objBase;
	Derived *objDerived;

	objBase = objDerived = new Derived;

	objDerived->fnVirtual ();
	/*************************************
	Calls fnVirtual of Derived.
	**************************************/

	objBase->fnVirtual ();
	/*************************************
	Still calls fnVirtual of Derived.
	**************************************/
}

Last edited on
Awesome, thank you, this answered all my questions, and yes, bet is a member of the Simple class.
Topic archived. No new replies allowed.