Help with friend functions


How would I do this without making the entire lunch class a friend of school? Whenever I tried friend functions I couldn't figure it out.

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include<iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

const int MAX = 25;
const short Size = 20;


class school
{
  public:
    school(short gridSize = MAX)
    {
      SetSize(getSize());
      clear(School);
      build(gridSize);
      print(gridSize, School);
    }

    school(); //default constructor

    friend class Lunch;
    void print(const int Size, const char  School[MAX][MAX]);
    void clear(char  School[MAX][MAX]);
    void build(const int Size);
    //Get function
    int getSize()const {return Size;}

    //Set function
    void SetSize(const int n);

  private:
    char School[MAX][MAX];
    short ActualSize;
};

class Lunch
{
  public:
    Lunch(int X = -1, int Y = -1);
    void place_me(school & skool, const int Size);
    void random_move(school & skool);

 private:
    int Xpos, Ypos;
    char LunchRep;
};


int main()
{
  srand(time(NULL));
  school s(Size);
  Lunch l(Size/2, Size/2);
  l.place_me(s, Size);
  for (int i = 0;i < 10;++i)
  {
    l.random_move(s);
  }

  return 0;
}

Lunch::Lunch(int X, int Y)
{
  Xpos = X;
  Ypos = Y;
  LunchRep = 'L';
}

void school::SetSize(const int n)
{
  ActualSize = n;
  return;
}


void Lunch::place_me(school & skool, const int Size)
{
  Xpos = (rand() % (Size-1)) + 1;
  Ypos = (rand() % (Size-1)) + 1;
  skool.School[Xpos][Ypos] = LunchRep;
}

void Lunch::random_move(school & skool)

{
  int Movement;

  Movement = rand() % 4;
  switch (Movement)
  {
    case 0:
      if (Ypos < skool.ActualSize - 1)
      {
        Ypos--;
      }
      break;
    case 1:
      if (Xpos < skool.ActualSize - 1)
      {
        Xpos++;
      }
      break;
    case 2:
      if (Ypos > 1)
      {
        Ypos++;
      }
      break;
    case 3:
      if (Xpos > 1)
      {
        Xpos--;
      }
      break;
  }
  cout << '(' << Xpos << ',' << Ypos << ')' << endl;
}


void school::print(const int Size, const char School[MAX][MAX])
{
  for(int i = 0;i < Size;i++)
  {
    for (int j = 0;j < Size;j++)
    {
      cout << School[i][j] << ' ';
    }

   cout << endl;
  }
  return;
}

void school::clear(char School[MAX][MAX])
{
  for (int i = 0;i < MAX;i++)
  {
    for (int j = 0;j < MAX;j++)
    {
      School[i][j] = '\0';
    }
  }
  return;
}


void school::build(const int Size)
{
  char Window = 'W', Wall = 'D';

  for(int i = 0; i < Size; i++)
  {
    for(int j = 0; j < Size; j++)
    {
      if(i == 0 || j == 0 || i == Size - 1 || j == Size - 1)
      {
        if ((i > 0 && i < Size - 1 && i % 5 == 0) || (j > 0 && j < Size - 1 && j % 5 == 0))
        {
          School[i][j] = Window;
        }
        else
        {
          School[i][j] = Wall;
        }
      }
      else
      {
        School[i][j] = ' ';
      }
    }
  }


return;
}
By "This" I mean make the above code compile and output the same as it does now.
I looked to it very quickly:
at line 83 the class Lunch is accessing
char **School;
but it is defined as private.
(there might be the same problem around... Just take the definition on line 23 away and see where the compiler complains)

You might want to either:
set it public (but there will not be control on the indexes or on the value of the char)
or
define Get and Set functions (as public in class school)
The get (which it seems you don't need) & set might look like this (you need to clean the following code to check boundaries, if you want, and adding the definition of the class in front of them)
1
2
3
4
5
6
7
8
9
10
11
12
char const c Get(int i, int j) const 
{
//perform controls to be sure that i and j are > 0 and < of MAX, if you need;
return School[i][j];
}

void Set(int i, int j, char value)
{
//perform controls to be sure that i and j are > 0 and < of MAX, if you need;
//perform controls on char value if you need
School[i][j] = value;
}

the following line
skool.School[Xpos][Ypos] = LunchRep;
should become
skool.Set(Xpos,Ypos,LunchRep);
Topic archived. No new replies allowed.