C++ 错误:重新定义矩阵类
c++ error: redefinition of matrix class
我一直在努力让这段代码编译。我正在尝试实现一个简单的矩阵类以用于另一个计算。
到目前为止,我陷入了以下两个错误:从编译器:
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’
我没有看到任何明显的重新定义。请帮忙
谢谢
以下是支持文件:
矩阵.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("%en",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: %en", 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 中使用"#include
guard"和C++头文件中的常见做法是防止重新定义。
例如,在您的情况下:
#ifndef MATRIX_H
#define MATRIX_H
class Matrix {
// [...]
};
#endif
此构造意味着,如果#include "Matrix.hh"
在源文件(及其include
d 文件)中多次出现,则class Matrix
只定义一次。
一些 C 和 C++ 实现提供了非标准指令#pragma once
(如注释中所建议的)。此指令插入到头文件的顶部,将确保该文件仅包含一次。出于可移植性原因,我更喜欢上面演示的结构。
相关文章:
- 使用命名空间时出现多个定义错误
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 链接阶段出现多重定义错误
- 避免模板类中的重定义错误
- 即使我没有包含多个文件,C++中的多个定义错误
- 跨多个类的全局变量而不会出现重定义错误?
- 尝试运行 wasm 函数时出现模块未定义错误
- C++ 预期的左大括号以及重定义错误
- C++:成员的越界声明必须是纯虚函数的定义错误
- c++中数组的未定义错误
- Visual C:模板类中的自定义错误消息
- 为什么C++模板不会导致多个定义错误?
- 只有一个定义/声明时标头声明变量的多堆定义错误
- C++ 在多个其他类中使用单个类 - 编译时出现多个定义错误
- 基类未定义.错误 C2504
- Q 斯坦达项重定义错误
- 可视化C++中的结构定义错误
- VC++ C2011 重定义错误 - 未使用的头文件