passing array into class by reference, having trouble with function and calls parameters

i dont really know where to put the arguments in DE_EDGE(the idea is to stop chars falling of the board), i managed to pass an array to a function before but it seems different to pass to class function

-1-create a function in class with a pointer to an array

-2-create a pointer to the array

-3-use the object to call with the array as a parameter, i get the error "no matching function for call to 'bunny::de_edge(char [10][10])'"

it says the matching candidate de_edge (char* (*)[10])




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
 #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cmath>
using namespace std;

class bunny
{
    public:

        bunny (int h = 0): age (h) {}

        int ageing (int h)
        {
            age++;
            return age;
        }

        void setsex (char s)
        {
            sex = s;
        }
        char getsex ()
        {
            return sex;
        }

        bunny  (int x=5, int y=5): posx (x), posy (y) {}

        void setname (string  x)
        {
            name = x;
        }
        string getname ()
        {
            return name;
        }
        int setposx (int x)
        {
            posx=x;
        }
        int setposy (int y)
        {
            posy=y;
        }

        void de_edge (char *array[10][10])//-1-  HERE IS DE_EDGE 
        {
             if (array [posx+1][posy]== "*")
            {
                posx-2;
            }
        }


        void hopabout ()
        {
         int box;
          box=1+(rand()%4);
        switch (box)
        {
            case 1:
            posx++;
            break;
            case 2:
            posx--;
            break;
            case 3:
            posy++;
            break;
            case 4:
            posy--;
            break;
        }
        }
        int getposx ()
        {
            return posx;
        }
        int getposy ()
        {
            return posy;
        }
    string name;
    int posx;
    int posy;
    char bunicon;
    char sex;
    int age;
    int box;
    };



char z = '*';
char x = ' ';
char brdray [10] [10] = {  {z,z,z,z,z,z,z,z,z,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z,z,z} };

int getrand ();       ///these guys are the function prototypes
char definesex ();
void getboard ();
string namelist ();
void createobj ();
void clearboard (char brdray [10][10]);



//////////////////////////////////////////////////////////////////////////////
int main()
{
char *array = &brdray[10][10]; //-2- here i point to the array
bool gameover = false;
bool gobreed = false;
srand(time(0));


vector <bunny>bunnies;
    for (int aa = 0;aa<8;aa++)
   {
        bunnies.push_back(bunny(0,0)) ;  //push back a default constructed bunny.
        bunny& workingBunny = bunnies.back() ;   // manipulate the last bunny in the vector
        workingBunny.setname(namelist()) ;
        workingBunny.setsex(definesex()) ;
        workingBunny.setposy(getrand());
        workingBunny.setposx(getrand());
        workingBunny.de_edge(array);//-3- here i hoped to pass to the class
        brdray [workingBunny.getposx()][workingBunny.getposy()] = (workingBunny.getsex());
    }
return 0;

}
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cmath>




using namespace std;

class bunny
{
    public:

        bunny (int h = 0): age (h) {}

        int ageing (int h)
        {
            age++;
            return age;
        }

        void setsex (char s)
        {
            sex = s;
        }
        char getsex ()
        {
            return sex;
        }

        bunny  (int x=5, int y=5): posx (x), posy (y) {}

        void setname (string  x)
        {
            name = x;
        }
        string getname ()
        {
            return name;
        }
        int setposx (int x)
        {
            posx=x;
        }
        int setposy (int y)
        {
            posy=y;
        }

        void de_edge (char array[10][10])// removed the  pointer
        {
             if (array [posx+1][posy]== '*') //change * to a char (single quotes NOT double quotes)
            {
                posx-2; //This statement has no effect! Recheck! You probably meant  posx-=2;
            }
        }


        void hopabout ()
        {
         int box; //The compiler will complain that this shadows a bunny class member called box
          box=1+(rand()%4);
        switch (box)
        {
            case 1:
            posx++;
            break;
            case 2:
            posx--;
            break;
            case 3:
            posy++;
            break;
            case 4:
            posy--;
            break;
        }
        }
        int getposx ()
        {
            return posx;
        }
        int getposy ()
        {
            return posy;
        }
    string name;
    int posx;
    int posy;
    char bunicon;
    char sex;
    int age;
    int box; //see comment above.
    };



char z = '*';
char x = ' ';
char brdray [10] [10] = {  {z,z,z,z,z,z,z,z,z,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z,z,z} };

int getrand ();       ///these guys are the function prototypes
char definesex ();
void getboard ();
string namelist ();
void createobj ();
void clearboard (char brdray [10][10]);



//////////////////////////////////////////////////////////////////////////////
int main()
{
//char array = &brdray[10][10]; //forget this
bool gameover = false;
bool gobreed = false;
srand(time(0));


vector <bunny>bunnies;
    for (int aa = 0;aa<8;aa++)
   {
        bunnies.push_back(bunny(0,0)) ;  
        bunny& workingBunny = bunnies.back() ; 
        workingBunny.setname(namelist()) ;
        workingBunny.setsex(definesex()) ;
        workingBunny.setposy(getrand());
        workingBunny.setposx(getrand());
        workingBunny.de_edge(brdray);//just pass the array name.
        brdray [workingBunny.getposx()][workingBunny.getposy()] = (workingBunny.getsex());
    }
return 0;

}


Arrays in C/C++ are pass by reference (address) anyway - you do not need (unless you have some real need) to pass a pointer to the array in the way you were attempting

Other issues ( I assume you will correct them as you have not completed the code yet - as you were having issues with the array passing)

1. The setposx and setposy functions are defined as returning an int - but you are not returning anything.

2. Function bodies for getrand, definesex, etc..
yayy it works thanks for your help, ur very clever
good eyes i might add
Topic archived. No new replies allowed.