Thread: Problem with .CPP program output HELP please!!!

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    4

    Unhappy Problem with .CPP program output HELP please!!!

    I am a beginner in C++ .i wrote this code in which operator is overloaded on matrices.Go thru the program;
    Code:
     :
                                     /*prog.cpp*/
    #include<iostream.h>
    #include<conio.h>
    int k=1;
    class matrix
    {
    	private:
    			static int a[20][20];
    			int m,n;
    	public:
    			friend istream& operator>>(istream &cin,matrix&);
    			friend ostream& operator<<(ostream &cout,matrix);
    			friend matrix operator+(matrix,matrix);
    			friend matrix operator-(matrix,matrix);
    			friend matrix operator*(matrix,matrix);
    			friend matrix operator/(matrix,matrix);
    };
    
    istream& operator>>(istream &cin,matrix &a)
    {
    	int i,j;
    	cout<<"Enter the size of the matrix m,n"<<endl;
    	cin>>a.m>>a.n;
    	cout<<"Enter the matrix"<<endl;
    	for(i=0;i<=a.m-1;i++)
    	for(j=0;j<=a.n-1;j++)
    	  cin>>a.a[i][j];
    	return(cin);
    }
    ostream& operator<<(ostream &cout,matrix a)
    {
    	int i,j;
    	cout<<"The matrix is as follows"<<endl;
    	for(i=0;i<=a.m-1;i++)
    	{
    	  for(j=0;j<=a.n-1;j++)
    	   cout<<a.a[i][j]<<"\t";
    	  cout<<"\n";
    	}
    	return(cout);
    }
    matrix operator+(matrix a,matrix b)
    {
    	matrix c;
    	int i,j;
    	c.m=a.m;
    	c.n=a.n;
    	for(i=0;i<=a.m-1;i++)
    	for(j=0;j<=a.n-1;j++)
    	c.a[i][j]=a.a[i][j]+b.a[i][j];
    	return(c);
    }
    
    matrix operator-(matrix a,matrix b)
    {
    	matrix c;
    	int i,j;
    	c.m=a.m;
    	c.n=a.n;
    	for(i=0;i<=a.m-1;i++)
    	for(j=0;j<=a.n-1;j++)
    	c.a[i][j]=a.a[i][j]-b.a[i][j];
    	return(c);
    }
    matrix operator*(matrix a,matrix b)
    {
    	matrix c;
    	int i,j;
    	c.m=a.m;
    	c.n=b.n;
    	if(a.n==b.m)
    	{
    	 for(i=0;i<=a.m-1;i++)
    	 for(j=0;j<=b.n-1;j++)
    	 for(k=0;k<=a.n-1;k++)
    	  c.a[i][j]+=a.a[i][k]+b.a[k][j];
    	}
    	 else
    	  k=0;
    	return(c);
    }
    matrix operator/(matrix a,matrix b)
    {
    	matrix c;
    	int i,j;
    	c.m=a.m;
    	c.n=a.n;
    	for(i=0;i<=a.m-1;i++)
    	for(j=0;j<=a.n-1;j++)
    	c.a[i][j]=(float)a.a[i][j]/float(b.a[i][j]);
    	return(c);
    }
    main()
    {
    	matrix a,b,c,d,e,f;
    	cin>>a>>b;
    	c=a+b;
    	d=a-b;
    	e=a*b;
    	f=a/b;
    	cout<<"The addition of matrices is:"<<"\n"<<c;
    	cout<<"The subtraction of matrices is:"<<"\n"<<d;
    	if(k==1)
    	cout<<"The multiplication of matrices is:"<<"\n"<<e;
    	else
    	cout<<"The multiplication of matrices is not possible"<<endl;
    	cout<<"The division of matrices is:"<<"\n"<<f;
    	getch();
    	return(0);
    }
    When i compile the above prog i get an error :
    "Linker error:undefined symbol matrix::a in module prog.cpp"

    please help me out with the error.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why is the member array a static?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    class matrix
    {
    	private:
    			static int a[20][20];

    u need to declare static int a[20][20] outside the class and use it directly.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    thanks 4 helping me out.The main problem was that a clas cannot have a static member.
    thx

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A class can have a static member, you just didn't implement it completely. In this case, though, the array shouldn't have been static anyway since each matrix instance should have its own data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Problem with program output
    By progrookie in forum C++ Programming
    Replies: 7
    Last Post: 03-23-2006, 02:58 PM
  3. sorting output on student info program
    By indigo0086 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 11:29 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Program abort due to memory problem
    By cazil in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2002, 12:55 PM