I have the following program :
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
template <class T>
class matrice
{
T **a;
int m,n;
public:
matrice();
matrice(int,int,int);
matrice(char *);
matrice(int,int,T **);
matrice(matrice &x);
~matrice(){};
template <class U>
friend ostream &operator <<(ostream &,matrice<U> &);
int getNrLinii(){
return m;
}
int getNrColoane(){
return n;
}
void setElement(int,int,T);
};
template <class T>
ostream &operator<<(ostream &os, matrice<T> &b){
int i,j;
for(i=0;i<b.m;i++){
for(j=0;j<b.n;j++){
os<<b.a[i][j]<<" ";
}
os<<endl;
}
return os;
}
matrice<char *>::matrice(int linii,int col,int ini=1):m(linii),n(col){
int i,j;
a=new char**[m];
for(i=0;i<m;i++){a[i]=new char*[n];}
if(ini){
for(i=0;i<m;i++)
for(j=0;j<n;j++){
cout<<"a["<<i<<"]["<<j<<"]= ";
char *tmp;
tmp=new char[100];
cin>>tmp;
a[i][j]=new char[strlen(tmp)+1];
strcpy(a[i][j],tmp);
delete tmp;
}
}
}
matrice<double >::matrice(int linii,int col,int ini=1):m(linii),n(col){
int i,j;
a=new double*[m];
for(i=0;i<m;i++){a[i]=new double[n];}
if(ini){
for(i=0;i<m;i++)
for(j=0;j<n;j++){
cout<<"a["<<i<<"]["<<j<<"]= ";
cin>>a[i][j];
}
}
}
matrice<char *>::matrice():m(1),n(1){
a=new char**[];
a[0]=new char*[1];
a[0][0]=new char[1];
strcpy(a[0][0],"");
}
matrice<double >::matrice():m(1),n(1){
int i,j;
a=new double*[1];
a[0]=new double[1];
a[0][0]=0;
}
matrice<double>::matrice(char *filename){
FILE *f;
f=fopen(filename,"r");
fscanf(f,"%i %i\n",&m,&n);
a=new double*[m];
for(int i=0;i<m;i++)a[i]=new double[m];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++)
fscanf(f,"%lf ",&(a[i][j]));
fscanf(f,"\n");
}
}
matrice<char*>::matrice(char *filename){
FILE *f;
f=fopen(filename,"r");
fscanf(f,"%i %i\n",&m,&n);
a=new char**[m];
for(int i=0;i<m;i++)a[i]=new char*[m];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
char *tmp;
tmp=new char[100];
fscanf(f,"%s ",&tmp);
a[i][j]=new char[strlen(tmp)+1];
strcpy(a[i][j],tmp);
delete tmp;
}
fscanf(f,"\n");
}
}
matrice<char *>::matrice(int linii,int col, char*** b):m(linii),n(col){
int i,j;
a=new char**[m];
for(i=0;i<m;i++){a[i]=new char*[n];}
for(i=0;i<m;i++)
for(j=0;j<n;j++){
cout<<"a["<<i<<"]["<<j<<"]= ";
a[i][j]=new char[strlen(b[i][j])+1];
strcpy(a[i][j],b[i][j]);
}
}
matrice<double >::matrice(int linii,int col,double **b):m(linii),n(col){
int i,j;
a=new double*[m];
for(i=0;i<m;i++){a[i]=new double[n];}
for(i=0;i<m;i++)
for(j=0;j<n;j++){
a[i][j]=b[i][j];
}
}
matrice<double>::matrice(matrice<double> &b):m(b.m),n(b.n){
a=new double*[m];
for(int i=0;i<m;i++){a[i]=new double[n];}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++){
a[i][j]=b.a[i][j];
}
}
matrice<char *>::matrice(matrice<char*> &b):m(b.m),n(b.n){
int i,j;
a=new char**[m];
for(i=0;i<m;i++){a[i]=new char*[n];}
for(i=0;i<m;i++)
for(j=0;j<n;j++){
cout<<"a["<<i<<"]["<<j<<"]= ";
a[i][j]=new char[strlen(b.a[i][j])+1];
strcpy(a[i][j],b.a[i][j]);
}
}
class matriceDouble:public matrice<double>{
public:
friend matriceDouble operator+(matriceDouble &a,matriceDouble &b){
if((a.m==b.m)&&(a.n==b.n)){
matriceDouble c=a;
for(int i=0;i<a.m;i++)
for(int j=0;j<a.n;j++)
c.a[i][j]=c.a[i][j]+b.a[i][j];
}
else{matriceDouble c;}
return c;
}
};
void main (int argc, char * const argv[]) {
//matrice<char *> m1(2,2);
matrice<double> m1("matrice1.txt");
matrice<double> m2("matrice2.txt");
cout<<m1;
cout<<endl;
cout<<m2<<endl;
matrice<double> m3=m2;
cout<<m3;
matriceDouble x;
getch();
}
For the matriceDouble I get this error in the friend operator+ procedure:
1>c:\documents and settings\administrator\desktop\proiect\proiect\proiect.cpp(232): error C2248: 'matrice<T>::m' : cannot access private member declared in class 'matrice<T>'
Why is this?
Thank you.