2013-03-12 2 views
0

클래스의 동적 배열에 대해 cout의 연산자 <<을 오버로드하려고합니다. 내 클래스와 멤버 함수는 다음대로 :클래스의 동적 배열에 대한 과부하 cout 연산자

class Matrix{ 
private: 
    int rows; 
    int columns; 
    double* matrix; 
public: 
    Matrix(); 
    explicit Matrix(int N); 
    Matrix(int M, int N); 
    void setValue(int M, int N, double value); 
    double getValue(int M, int N); 
    bool isValid() const; 
    int getRows(); 
    int getColumns(); 
    ~Matrix(); 
    friend ostream& operator<<(ostream &out, const Matrix&matrix1); 

};

Matrix::Matrix(){ 
    matrix = NULL; 
} 

Matrix::Matrix(int N){ 
    matrix = new double[N * N]; 
    rows = N; 
    columns = N; 

    for(int i = 0; i < N; i++){ 
     for(int j = 0; j < N; j++){ 
      if(i==j) 
       matrix[i * N + j] = 1; 
      else 
       matrix[i * N + j] = 0; 
     } 
    } 
} 

Matrix::Matrix(int M, int N){ 
    matrix = new double[M * N]; 
    rows = M; 
    columns = N; 

    for(int i = 0; i < M; i++){ 
     for(int j = 0; j < N; j++) 
      matrix[i * N + j] = 0; 
    } 
} 

Matrix::~Matrix(){ 
    delete [] matrix; 
} 

void Matrix::setValue(int M, int N, double value){ 
    matrix[M * columns + N] = value; 
} 

double Matrix::getValue(int M, int N){ 
    return matrix[M * columns + N]; 
} 

bool Matrix::isValid() const{ 
    if(matrix==NULL) 
     return false; 
    else 
     return true; 
} 

int Matrix::getRows(){ 
    return rows; 
} 

int Matrix::getColumns(){ 
    return columns; 
} 

나는 다음과 같이 < < 연산자를 구현하기 위해 노력했습니다 :

ostream& operator<<(ostream &out, const Matrix&matrix1){ 
Matrix mat1; 
int C = mat1.getColumns(); 
int R = mat1.getRows(); 

for(int i = 0; i < R; i++){ 
    for(int j = 0; j < C; j++) 
     out << mat1.getValue(i,j) << "\t"; 
    out << endl; 
} 
return out; 

}

와 함수에서 호출 :

void test(){ 
Matrix mat1(3,4); 
cout << mat1 << endl; 

}

하지만 아무것도 인쇄되지 않습니다. 과부하 기능이 CR에 대한 값을 얻지 못하는 것 같지만 잘못되었을 수 있습니다. 누구나 아이디어가 있어요?

이 형태 당신은 matrix1의 내용을 인쇄 할 필요가

a11  a12  a13 . . . 
a21  a22  a23 . . . 
.  .  .  . . . 
.  .  .  . . . 
.  .  .  . . . 
+0

"과부하 기능이 C 및 R에 대한 값을 얻지 못하는 것 같지만 잘못된 것 같습니다."- 중단 점을 설정하고 C 및 R의 값을 확인 했습니까? – chris

+0

이전에 생성자를 사용하는 것처럼 불필요한 지역 변수를 생성하는 중입니다. – molbdnilo

+0

아니, 나는 팁을 주셔서 감사하지 않았다! – Ole1991

답변

2

matrix1을 부여하지. 나는이 권리를보고

ostream& operator<<(ostream &out, const Matrix& matrix1) 
{ 
    //Matrix mat1; // <- Comment ythis 
    int C = matrix1.getColumns(); // <<- matrix1 
    int R = matrix1.getRows(); // <<- matrix1 

    for (int i = 0; i < R; i++) 
    { 
     for (int j = 0; j < C; j++) 
      out << matrix1.getValue(i, j) << "\t"; // <<- matrix1 
     out << endl; 
    } 
    return out; 
} 
2

에서 동적 매트릭스를 인쇄한다고 가정한다. 로컬 비어있는 행렬 mat1을 만들고 그 내용을 대신 인쇄하고 있습니다. 당신은 빈 mat1 인쇄

ostream& operator<<(ostream &out, const Matrix& matrix1) 
{ 
    int C = matrix1.getColumns(); 
    int R = matrix1.getRows(); 

    for(int i = 0; i < R; i++){ 
    for(int j = 0; j < C; j++) 
     out << matrix1.getValue(i,j) << "\t"; 
    out << endl; 
    } 
    return out; 

} 
+0

Ofcourse ... 고마워요! – Ole1991

2

오전, 행렬 1 연산자 < <에 매개 변수입니다하지만 당신은 새로운 MAT1를 생성하고 그 출력된다?

+0

이 권한이 있습니다.) –