2012-12-17 2 views
0

이 코드를 컴파일하는 데 어려움을 겪고 있습니다. 다른 계산에 사용할 간단한 Matrix 클래스를 구현하려고합니다.C++ 오류 : 행렬 클래스의 재정의

은 지금부터 나는 다음과 같은 두 가지 오류에 붙어 : 를 컴파일러에서 :

g++ -g -Wall -c main.cpp 
In file included from main.cpp:19:0: 
    Matrix.hh:2:7: error: redefinition of ‘class Matrix’ 
    Matrix.hh:2:14: error: previous definition of ‘class Matrix’ 

내가 명백한 재정의가 표시되지 않습니다. 도와주세요

감사

다음

있는 지원 파일 :

Matrix.hh :

// Matrix class of variable size 
class Matrix { 

private: 
    int n_rows; 
    int n_cols; 
    double *array; 

public: 
    // Constructors 
    Matrix(); // default constructor 
    Matrix(int rows, int cols); // two-argument constructor 
// Matrix(const Matrix &arr); // copy constructor 


    // Destructor 
    ~Matrix(); 

    // Mutators 
// void add(Matrix m2); 
// void subtract(Matrix m2); 
    void setelem(int r, int c, double val); 

    // Accessors 
    int getrows(); 
    int getcols(); 
    double getelem(int r, int c); 
    bool equals(Matrix m2); 
    char display(); 
    int WriteToArray(double *CopyOfArray); 

}; 

Matrix.cc :

#include "Matrix.hh" 
#include <cassert> 
#include <math.h> 
#include <string.h> 
#include <stdio.h> 

// CONSTRUCTORS 
// default constructor 
Matrix::Matrix() { 
    n_rows = 0; 
    n_cols = 0; 
    array = NULL; 
} 

// two-argument constructor 
Matrix::Matrix(int rows, int cols) { 
    n_rows = rows; 
    n_cols = cols; 
    array = new double[rows * cols]; 

    for (int i = 0; i < n_rows; i++) { 
     for (int j = 0; j < n_cols; j++) { 
      array[i * n_cols + j] = 0; 
     } 
    } 
} 

/* copy constructor*/ 
//Matrix::Matrix(const Matrix &arr) { 
// n_rows = arr.n_rows; // find proper size 
// n_cols = arr.n_cols; // find proper size 
// array = new double[n_rows * n_cols]; // allocate a deep-copy 
// for (int i = 0; i < n_rows; i++) { 
//  for (int j = 0; j < n_cols; j++) { 
//   array[i * n_cols + j] = arr.array[i * n_cols + j]; 
//  } 
// } 
// arr=&array; 
//} 


// DESTRUCTOR 
Matrix::~Matrix() { 
    assert(array[1]!=0); 
    int ii=0; 
    printf("array values in ~\n"); 
    for(ii=0;ii<n_rows;ii++){ 
     printf("%e\n",array[ii]); 
    } 
    delete[] array; // free up memory 
} 

// MUTATORS 
// adds a second matrix to this matrix object 
void Matrix::add(Matrix m2) { 
    assert (m2.n_rows == n_rows); // assert dimensions match 
    assert (m2.n_cols == n_cols); // assert dimensions match 
    for (int i = 0; i < n_rows; i++) { 
     for (int j = 0; j < n_cols; j++) { 
      array[i * n_cols + j] = array[i * n_cols + j] + m2.array[i *n_cols + j]; 
     } 
    } 
} 

// subtracts a second matrix to this matrix object 
void Matrix::subtract(Matrix m2) { 
assert (m2.n_rows == n_rows); // assert dimensions match 
assert (m2.n_cols == n_cols); // assert dimensions match 
for (int i = 0; i < n_rows; i++) { 
    for (int j = 0; j < n_cols; j++) { 
     array[i * n_cols + j] = array[i * n_cols + j] - m2.array[i *n_cols + j]; 
     } 
    } 
} 

// change an element in the matrix 
void Matrix::setelem(int r, int c, double val) { 

    array[r * n_cols + c] = val; 
} 

// ACCESSORS 
// get number of rows 
int Matrix::getrows() { 
    return n_rows; 
} 

// get number of columns 
int Matrix::getcols() { 
    return n_cols; 
} 

// get value of element at specified row, col 
double Matrix::getelem(int r, int c) { 
    printf("getelem value: %e\n", array[r*n_cols+c]); 
    return array[r * n_cols + c]; 
} 

// test if two matrices are equal 
bool Matrix::equals(Matrix m2) { 
    // if dimensions not equal, matrices not equal 
    if (m2.n_rows != n_rows || m2.n_cols != n_cols) { 
     return false; 
    } 

    // test equality element by element 
    for (int i = 0; i < n_rows; i++) { 
     for (int j = 0; j < n_cols; j++) { 
      if (array[i * n_cols + j] != m2.array[i * n_cols + j]) { 
       return false; // if one val not equal, matrices not equal 
      } 
     } 
    } 

    return true; // has checked all vals, matrices match 
} 

char Matrix::display() { 
    // Print out the contents of this matrix m2: 
    char string[100], temp[1]; 
    int n; 
    for(int r = 0; r < n_rows; r++) { 
     for(int c = 0; c < n_cols; c++) { 
     printf("Element (%d, %d) = %e, \n", r,c,array[r * n_cols + c]); 

     } 
    } 
    printf("\n"); 
    return *string; 
} 

int Matrix::WriteToArray(double *CopyOfArray){ 
    int i=0; 
    while(i<n_rows){ 
     *(CopyOfArray+i)=array[i*n_cols]; 
     i++; 
    } 
    return *CopyOfArray; 
} 
+1

경호원이 있습니까? – Pubby

+0

main.cpp도 포함 시키십시오. 문제를 재현하는 해골 중 하나가 –

+0

이고 @Pubby가 제안하는대로'Matrix.hh '의 맨 위에'#pragma once'를 추가하여 도움이되는지 확인하십시오. –

답변

2

그것은 "를 사용하는 것이 일반적이다 #include 가드 "C 및 C++ 헤더 파일에서 재 정의를 방지합니다. 귀하의 경우 예를 들어

는 :

#ifndef MATRIX_H 
#define MATRIX_H 

class Matrix { 
    // [...] 
}; 

#endif 

이 구조는 #include "Matrix.hh" 소스 파일에 두 번 이상 나타납니다 (및 include D 파일) 경우, 다음 class Matrix는 오직 한 번 정의 된 것을 의미한다.

일부 C 및 C++ 구현은 비표준 지시문 #pragma once (주석에 제안 된대로)을 제공합니다. 이 지시문은 헤더 파일의 맨 위에 삽입되어 파일이 한 번만 포함되도록합니다. 필자는 이식성을 이유로 위에서 설명한 구조를 선호합니다.

+1

고마워, 고쳐 줬어. – user1909356