guys
i am trying to write 3x3 matrix . i am posting the main,.h and .cpp file,main and .h file is given and i have to write .cpp file. can sombody look at it and suggest me solution
appricated guys
IntMatrix3_h

#ifndef INTMATRIX3_H
#define INTMATRIX3_H

#include<iostream>
#include<cmath>
using namespace std;

typedef int* IntArrayPtr;
//class declaration
class CIntMatrix3
{
	public:
	//friend function prototypes
//friend CIntMatrix3 operator - (const CIntMatrix3& pMatrix); 
friend CIntMatrix3 operator + (const CIntMatrix3& pLeftMatrix,const CIntMatrix3& pRightMatrix);
friend CIntMatrix3 operator - (const CIntMatrix3& pLeftMatrix,const CIntMatrix3& pRightMatrix);
//friend bool operator == (const CIntMatrix3& pLeftMatrix,const CIntMatrix3& pRightMatrix);
//assumes that column of left hand matrix is equal to the row of right hand matrix

//friend CIntMatrix3 operator * (const CIntMatrix3& pLeftMatrix,const CIntMatrix3& pRightMatrix);

friend istream& operator >>(istream& instream, CIntMatrix3& pMatrix);
friend ostream& operator <<(ostream& outstream, CIntMatrix3& pMatrix); 


	//constructors
	CIntMatrix3(void);//default constructor
	CIntMatrix3 (IntArrayPtr,IntArrayPtr,IntArrayPtr);//parametrized constructor
    CIntMatrix3 (const CIntMatrix3&);//copying constructor
	~CIntMatrix3 (void);

	//accessor function
	void setElementAt (int,int,int);
	int getElementAt(int,int)const;

	//other member function
	void operator = (const CIntMatrix3&);
private:
	//data members
	int size;
	IntArrayPtr* element;
};

#endif //INTMATRIX3_H

main.cpp

#include<iostream>
#include<cctype>
#include<cstdio>
using namespace std;
#include "IntMatrix3.h"

void main(void)
{
	CIntMatrix3 a,b,c,*d;

	cout<< "Get value for matrix a:\n";
	cin>> a;
	cout<< "a = ";
	cout<< a;
	cout<< endl;


	cout<< "Get value for matrix b:\n";
	cin>> b;
	cout<<"b = ";
	cout<< b;
	cout<< endl;

	cout<< "a+b = ";
	c = a+b;
	cout<< c;
	cout<<endl;

	/*cout<<"a-b = ";
	cout<< a-b;
	cout<<endl;

	cout<< "(a+b)[2,2] = " <<c.getElementAt(1,1)<< endl << endl;
	cout<< "\nReset (a+b) [2,2] to 0\n\n";
	c.setElementAt(1,1,0);

	d = new CIntMatrix3();
	*d = c;
	cout<< "(a+b) [2,2] = " << d->getElementAt(1,1) << endl << endl;
	cout<< endl;
    
	cout<< "a*b = ";
	cout<< a*b;
	cout<< endl;

    CIntMatrix3 e(-a);
	cout<< "-a = ";
	cout<< e;
	cout<<endl;

	if (e== a)
		cout<<"e and a are same.\n";
	else
		cout<<" e and a are different.\n";*/

	delete d;
}//end of main

IntMatrix3.cpp

#include<iostream>
using namespace std;
#include "IntMatrix3.h"

CIntMatrix3::CIntMatrix3(void)
{
	//Body empty
}

CIntMatrix3::CIntMatrix3(IntArrayPtr row1,IntArrayPtr row2,IntArrayPtr row3)
{
	  element = new IntArrayPtr[3];
	 element = &row1;
	 row1 = row2;
	 row2 = row3;

}
CIntMatrix3::CIntMatrix3 (const CIntMatrix3& x)
{
	size = x.size;
   for(int i=1; i<3; i++)
      for(int j=1; j<3; j++)
         element[i][j] = x.element[i][j];

}
CIntMatrix3::~CIntMatrix3 (void)
{
	delete [] element;
}

void CIntMatrix3::setElementAt(int row,int col,int s)
{

}
int CIntMatrix3::getElementAt(int row,int col)const
{
	return element[row][col];

}
CIntMatrix3 operator + (const CIntMatrix3& a,const CIntMatrix3& b)
{ 
   CIntMatrix3 temp;
	for (int i = 0; i<3;i++)
		for (int j = 0; j<3;j++)
			temp.size[i][j] = a.size[i][j] + b.size[i][j];
		return temp;
}
CIntMatrix3 operator - (const CIntMatrix3& a,const CIntMatrix3& b)
{ 
   CIntMatrix3 temp;
	for (int i = 0; i<3;i++)
		for (int j = 0; j<3;j++)
			temp.size[i][j] = a.size[i][j] - b.size[i][j];
		return temp;
}

istream& operator >>(istream& ins, CIntMatrix3& m)
{
	for (int i=0; i<3; i++)
		cout<<"Enter three value for row# "<<i+3;
	for (int j=0; j<3; j++)
		ins>> m[i][j];
	return ins;
}
ostream& operator <<(ostream& outs, CIntMatrix3& m)
{
	for (int i=0; i<3; i++)
		outs<<"[\t";
	for (int j=0; j<3; j++)
		outs<< m[i][j]<<"\t";
	if (i==3)
		outs<<"]";
	else
		outs<<endl;
	return outs;
}

error

--------------------Configuration: IntMatrix3 - Win32 Debug--------------------
Compiling...
IntMatrix3.cpp
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(47) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(47) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(47) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(47) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(47) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(47) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(47) : error C2106: '=' : left operand must be l-value
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(55) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(55) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(55) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(55) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(55) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(55) : error C2109: subscript requires array or pointer type
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(55) : error C2106: '=' : left operand must be l-value
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(64) : error C2676: binary '[' : 'class CIntMatrix3' does not define this operator or a conversion to a type acceptable to the predefined operator
C:\cworkSUMMER\MATRIX\IntMatrix3.cpp(72) : error C2676: binary '[' : 'class CIntMatrix3' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.

IntMatrix3.obj - 16 error(s), 0 warning(s)

Recommended Answers

All 2 Replies

>temp.size[j]
size is an int, not an two dimensional array. Fix this, rinse, and repeat until your errors go away. Though just getting a clean compile doesn't mean it will work like you want it to. ;)

thx for the great help narue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.