Public method access from global functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

class A
{
public:
	int a;
	int get_a()
	{
		return a;
	}
};

void access_get_a()
{
	int temp = get_a();
}

void main()
{};


Compilation of the above code gives: error C2065: 'get_a' : undeclared identifier. What's the problem with the above code?

Thanks.
Should be smth. like this:

void access_get_a()
{
A a;
int temp = a.get_a();
}
If you want a "global method access" then the object has also to be a global variable. Making it a local variable always return the initialization value!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

class A
{
private:
	int a; // keep the variable private, otherwise you wouldn't need the "get_a" function!
public:
	int get_a()
	{
		return a;
	}
};

A a ; // the global object.

void access_get_a()
{
	int temp = a.get_a(); // here we access the global object
}

void main()
{
    access_get_a() ;
};


Although this should compile, this is most likely not the best solution.
But to find a better one, I need more information ;-).
Thanks Onur!
Topic archived. No new replies allowed.