-1
동적 객체를 만들고 조작하는 클래스 프로젝트가 있습니다. 2 차원 포인터 배열을 사용하여 Complex 유형의 객체 (복소수 클래스)를 저장하는 Matrix라는 클래스가 있습니다. 배열의 모든 값을 함께 추가하고 새 배열을 반환하여 2 개의 배열을 추가 할 수 있어야합니다. 문제는 배열의 각 Complex 객체에 액세스하는 구문을 이해할 수 없다는 것입니다. Matrix 객체에 대한 오버로드 입력 연산자 여기2D 동적 배열 추가
const Matrix Matrix::operator+(const Matrix& rhs) const
{
Matrix newMatrix(mRows,mCols);
for(int i=0;i<mRows;i++)
{
for(int j=0;j<mCols;j++)
{
(*newMatrix.complexArray[i]) = (*complexArray[i])+ (*rhs.complexArray[i]);
}
}
return newMatrix;
}
됩니다 : 여기
istream& operator>>(istream& input, Matrix& matrix)
{
bool inputCheck = false;
int cols;
while(inputCheck == false)
{
cout << "Input Matrix: Enter # rows and # columns:" << endl;
input >> matrix.mRows >> cols;
matrix.mCols = cols/2;
//checking for invalid input
if(matrix.mRows <= 0 || cols <= 0)
{
cout << "Input was invalid. Try using integers." << endl;
inputCheck = false;
}
else
{
inputCheck = true;
}
input.clear();
input.ignore(80, '\n');
}
if(inputCheck = true)
{
cout << "Input the matrix:" << endl;
for(int i=0;i< (matrix.mRows+matrix.mCols);i++)
{
Complex* newComplex = new Complex();
input >> *newComplex;
matrix.complexArray[i] = newComplex;
}
}
return input;
}
는 매트릭스 클래스 정의된다
class Matrix
{
friend istream& operator>>(istream&, Matrix&);
friend ostream& operator<<(ostream&, const Matrix&);
private:
int mRows;
int mCols;
static const int MAX_ROWS = 10;
static const int MAX_COLUMNS = 15;
Complex **complexArray;
public:
Matrix(int=0,int=0);
Matrix(Complex&);
~Matrix();
Matrix(Matrix&);
Matrix& operator=(const Matrix&);
const Matrix operator+(const Matrix&) const;
};
여기에 지금까지 오버로드 또한 연산자가 무엇인가
그리고 생성자 :
Matrix::Matrix(int r, int c)
{
if(r>0 && c>0)
{
mRows = r;
mCols = c;
}
else
{
mRows = 0;
mCols = 0;
}
if(mRows < MAX_ROWS && mCols < MAX_COLUMNS)
{
//complexArray= new Complex[mRows];
complexArray= new Complex*[mRows];
for(int i=0;i<mRows;i++)
{
complexArray[i] = new Complex[mCols];
}
}
}
현재와 같이 프로그램이 컴파일되지만 런타임 중에 행렬을 추가하면 작동을 멈 춥니 다. 누구나 사용해야하는 구문과 이유를 알 수 있다면 매우 유용 할 것입니다.