My friend function is not letting me use a function in a class from another class. What am I doing wrong?

I am trying to use the disphanddealer in Player.cpp, but it is saying that it is not declared. I thought that since I put "friend class Dealer" into player.h, I would be able to use the functions in dealer.h? Am I wrong?

Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef PLAYER_H
#define PLAYER_H


class Player
{
    public:
        friend class Dealer;

        Player();

        void deal();
        void disphand();
        void face();
        void hit();

    protected:
    private:
        int card1, card2;
        char card1f, card2f;

};

#endif // PLAYER_H 


Dealer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef DEALER_H
#define DEALER_H


class Dealer: public Player
{
    public:
        friend class Player;

        Dealer();

        void disphanddealer();
    protected:
    private:
        int card1, card2;
        char card1f, card2f;
};

#endif // DEALER_H 


Player.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "Player.h"
#include "Dealer.h"
using namespace std;

Player::Player()
{
}

void Player::deal()
{
    card1 = (rand()%13) + 2;
    card2 = (rand()%13) + 2;

    face();
    disphand();

}

void Player::disphand()
{
        if ((card1 < 11) && (card2 < 11))
        cout << card1 << " " << card2 << endl << endl;
    else if ((card1 < 11) && (card2 >= 11))
        cout << card1 << " " << (char) card2 << endl << endl;
    else if ((card1 >= 11) && (card2 < 11))
        cout << (char) card1 << " " << card2 << endl << endl;
    else if ((card1 >= 11) && (card2 >= 11))
        cout << (char) card1 << " " << (char) card2 << endl << endl;
}

void Player::face()
{
        if (card1 == 11)
        {
            card1 = 'J';
        }
    else if (card1 == 12)
        {
            card1 = 'Q';
        }
    else if (card1 == 13)
        {
            card1 = 'K';
        }
    else if (card1 == 14)
        {
            card1 = 'A';
        }
    else if (card2 == 11)
        {
            card2 = 'J';
        }
    else if (card2 == 12)
        {
            card2 = 'Q';
        }
    else if (card2 == 13)
        {
            card2 = 'K';
        }
    else if(card2 == 14)
        {
            card2 = 'A';
        }

}

void Player::hit()
{
    system("CLS");
    disphanddealer();
    disphand();

}


Dealer.cpp
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "Player.h"
#include "Dealer.h"
using namespace std;

Dealer::Dealer()
{
    srand(time(0));

    card1 = (rand()%13) + 2;
    card2 = (rand()%13) + 2;

    face();
    disphanddealer();
}

void Dealer::disphanddealer()
{
    char x = 'x';

    if ((card1 < 11) && (card2 < 11))
        cout << x << " " << card2 << endl << endl;
    else if ((card1 < 11) && (card2 >= 11))
        cout << x << " " << (char) card2 << endl << endl;
    else if ((card1 >= 11) && (card2 < 11))
        cout << x << " " << card2 << endl << endl;
    else if ((card1 >= 11) && (card2 >= 11))
        cout << x << " " << (char) card2 << endl << endl;

}


I am getting an error on line 75 of player.cpp ("disphanddealer was not declared in this scope")

Also, if anyone can tell me, sometimes when I run it, the dealer displays a random character (like a music note, or an alpha) rather than a number(or J, Q, K, or A). Does anyone know why this is?
Last edited on
Here is a bit help about how to use friend function..
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
class B
{
    // B declares A as a friend...
    friend class A;
 
private:
    void xyz()
    {
        std::cout << "in B's xyz";
    }
};
 
class A
{
public:
    A()
    {
        B b;
        // ... and A now has access to B's private members
        b.xyz();
    }
};
 
int main()
{
    A a;
    return 0;
}
You need to call disphanddealer() with a dealer object. You're just trying to call it as if it were a global function.

You are getting weird characters displayed because the ASCII characters between 11 and 13 are not printable, and you are trying to display them by casting those numbers to characters.
Topic archived. No new replies allowed.