illegal reference to non-static member

Hi friends,

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
lass SUS{
	int x;
	int y;
public:
	SUS(int xx)
	{
		x = xx;
		y = xx;
	}
	friend class names;

};


class Tam{
	char *p;
	char *s;
public:
	Tam(char *ss)
	{

		p = new char[strlen(ss) + 1];
		strcpy(p, ss);
	}
	friend class names;
};



class names{
	int a;
public:
	void display()
	{
		cout << SUS::x << endl;
		cout << Tam::p << endl;		
	}
};


my aim.
sss class at "int x" and the class ddd "char * p" I want access to the class names through.

sss class at "int y" and the class ddd "char * ps" I do not want to have access to a class of names.

but


gives this error "illegal reference to non-static member"

when I do static variables.problem solved.
How do I solve this problem.

best regards.



Last edited on
You might want to try making an instance of class Tam and class SUS in names and then use them to get the variables, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class names{
	int a;
        Tam *t;
        SUS *s;
public:
         names(/*Default ctor*/)
        {
               this->t = new Tam(/*Parameters here*/);
               this->s = new SUS(/*Parameters here*/);

          }

       ~names(){ delete t; delete s;} //destructor
	void display()
	{
		cout << s->x << endl;
		cout << t->p << endl;		
	}
};


or you can try to make the variables in Tam and SUS Static
Last edited on
thanks

but, I want to do this is not.two elements can be accessed in this way.
I do not want to have access to y and s..
The error messages is telling you what to do: illegal reference to non-static member
You can use members like that only if they are static
Can not be accessed by friends that are not static elements.?
If you want to access some member like ClassName::MemberName, MemberName must be static
How do I access otherwise.
Is there another way to
ObjectName.MemberName
or
PointerName->MemberName
How else can I use my example.
thanks Bazzy
Topic archived. No new replies allowed.