how define function outside of class?

Write your question here.
I trying to define a operator that use friend function, but not inside of class. I want outside of class. Help me
My english is bad ;))
My code, defined inside.
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
class myarray
{
private:
	char *nameofarray;
	int **a;
	int m;
	int n;
public:
	friend istream& operator >>(istream& cin, myarray &x)
	{
		srand(time(0));
		x.nameofarray = new char [20];
		cout<<"Enter array's name : ";
		cin.ignore(1);
		cin.getline(x.nameofarray,20);
		cout<<"Enter m,n : ";
		cin>>x.m>>x.n;
		cout<<"Your array have been init!!!"<<endl;
		x.a=new int *[x.m];
		for (int i=0;i<x.m;++i)
		{
			x.a[i]=new int [x.n];
			for (int j=0;j<x.n;++j)
			{
				x.a[i][j]=rand()%10;
			}
		}
		return cin;
	}
};
Do you mean:
1
2
3
4
5
6
void bar() {}

class Foo
{
  friend void bar();
}
Last edited on
Instead of

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public:
	friend istream& operator >>(istream& cin, myarray &x)
	{
		srand(time(0));
		x.nameofarray = new char [20];
		cout<<"Enter array's name : ";
		cin.ignore(1);
		cin.getline(x.nameofarray,20);
		cout<<"Enter m,n : ";
		cin>>x.m>>x.n;
		cout<<"Your array have been init!!!"<<endl;
		x.a=new int *[x.m];
		for (int i=0;i<x.m;++i)
		{
			x.a[i]=new int [x.n];
			for (int j=0;j<x.n;++j)
			{
				x.a[i][j]=rand()%10;
			}
		}
		return cin;
	}
};


write simply

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
public:
	friend istream& operator >>(istream& cin, myarray &x);
};

istream& operator >>(istream& cin, myarray &x)
{
	srand(time(0));
	x.nameofarray = new char [20];
	cout<<"Enter array's name : ";
	cin.ignore(1);
	cin.getline(x.nameofarray,20);
	cout<<"Enter m,n : ";
	cin>>x.m>>x.n;
	cout<<"Your array have been init!!!"<<endl;
	x.a=new int *[x.m];
	for (int i=0;i<x.m;++i)
	{
		x.a[i]=new int [x.n];
		for (int j=0;j<x.n;++j)
		{
			x.a[i][j]=rand()%10;
		}
	}
	return cin;
}

Last edited on
Topic archived. No new replies allowed.