簡體   English   中英

c ++錯誤:重新定義矩陣類

[英]c++ error: redefinition of matrix class

我一直在努力讓這個代碼編譯。 我試圖實現一個簡單的Matrix類用於另一個計算。

截至目前,我遇到了以下兩個錯誤:來自編譯器:

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;
}

通常的做法是在C和C ++頭文件中使用“ #include guards”來防止重新定義。

例如,在您的情況下:

#ifndef MATRIX_H
#define MATRIX_H

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

#endif

這個結構意味着如果#include "Matrix.hh"在源文件中出現不止一次(並且它include d個文件),那么class Matrix只被定義一次。

一些C和C ++實現提供非標准指令#pragma once (如注釋中所建議)。 插入頭文件頂部的該指令將確保該文件僅包含一次。 出於可移植性的原因,我更喜歡上面演示的構造。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM