how define function outside of class?
Jun 8, 2013 at 2:47pm UTC
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;
}
};
Jun 8, 2013 at 3:01pm UTC
Do you mean:
1 2 3 4 5 6
void bar() {}
class Foo
{
friend void bar();
}
Last edited on Jun 8, 2013 at 3:02pm UTC
Jun 8, 2013 at 3:05pm UTC
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 Jun 8, 2013 at 3:06pm UTC
Topic archived. No new replies allowed.