class, main, and main 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
29
30
31
32
33
34
35
36
class Cexample{
   public:
     virtual int example();
     int xyLoc[50][25];
   private:
     i, j;
};
int Cexample::example()
{
        for(i = 34; i<46; i++){
		for(j=3; j<23; j++){
			if(i < 35 || i > 44){
				xyLoc[i][j] = 1;
			}
			else if(((i>34 && i<45))&&(j<4 || j>21))
				xyLoc[i][j] = 1;
		}//end for
	}//end for
        xyLoc[37][15] = 1;
        xyLoc[38][13] = 1;
        xyLoc[39][14] = 0;
}
void deleteLoc()
{
    CExample ex;
    ex.xyLoc[37][15] = 0; //heres the problem, xyLoc[37][15] doesn't change its
                          //value, it is still 1,
                          //so the function called by main cannot change the
                          //value of the function of a class? What is the proper
                          //way of changing the value of the function of the
                          //class? using the function called by main.
}

int main(){
   deleteLoc();
}


Thanks... look at the comment in my 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
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

class Cexample{
   public:
     virtual int example();
     int xyLoc[50][25];
   private:
     int i, j;
};
int Cexample::example()
{
        for(i = 34; i<46; i++){
		for(j=3; j<23; j++){
			if(i < 35 || i > 44){
				xyLoc[i][j] = 1;
			}
			else if(((i>34 && i<45))&&(j<4 || j>21))
				xyLoc[i][j] = 1;
		}//end for
	}//end for
        xyLoc[37][15] = 1;
        xyLoc[38][13] = 1;
        xyLoc[39][14] = 0;

        return 0;
}
void deleteLoc()
{
    Cexample ex;
    ex.xyLoc[37][15] = 0; //heres the problem, xyLoc[37][15] doesn't change its
                          //value, it is still 1,
                          //so the function called by main cannot change the
                          //value of the function of a class? What is the proper
                          //way of changing the value of the function of the
                          //class? using the function called by main.
    cout << ex.xyLoc[37][15] << endl;
}

int main(){
   deleteLoc();
   return 0;
}

Output
0
yeah I don't understand the question.

Also -- what does Cexample::example have to do with this? You're never calling that function. I don't see how it fits in this question.
Well, gcampton's answer might still be a little obtuse to you...

The reason you see no change in main() is because the value was changed in an instance of the class local to your function.

If you want to change something from main(), you have to tell the function what object you wish to modify.

You can create a global object and manipulate that directly.
1
2
3
4
5
6
7
8
9
10
11
12
Cexample ex;

void deleteLoc()
{
    ex.xyLoc[37][15] = 0;
}

int main()
{
    deleteLoc();
    return 0;
}

Or you can pass a reference or pointer to your object as argument to the function.
1
2
3
4
5
6
7
8
9
10
11
void deleteLoc( Cexample& ex )
{
    ex.xyLoc[37][15] = 0;
}

int main()
{
    Cexample cexample;
    deleteLoc( cexample );
    return 0;
}

Or you can make your function a member of the object itself.
1
2
3
4
5
6
7
8
9
10
11
12
...
void Cexample::deleteLoc()
{
    xyLoc[37][15] = 0;
}

int main()
{
    Cexample ex;
    ex.deleteLoc();
    return 0;
}

Hope this helps.
Thanks Duoas!
I already tried this method before you posted it :)
1
2
3
4
5
6
7
8
9
10
11
void Cexample::deleteLoc()
{
    xyLoc[37][15] = 0;
}

int main()
{
    Cexample ex;
    ex.deleteLoc();
    return 0;
}

and it works fine...
but I want your second solution...
1
2
3
4
5
6
7
8
9
10
11
void deleteLoc( Cexample& ex )
{
    ex.xyLoc[37][15] = 0;
}

int main()
{
    Cexample cexample;
    deleteLoc( cexample );
    return 0;
}

I'll try this one. THanks
Topic archived. No new replies allowed.