我是c ++编程的新手(实际上是一般的编程),而且我在c ++程序中遇到了一些复制构造函数的问题,该程序应该证明矩阵的添加。
我认为我的程序没有到达复制构造函数,但我不知道为什么。我理解为什么如果我没有定义复制构造函数,我应该期望这不起作用,因为析构函数会删除matrix result
指向的数据。但是,我已经定义了一个拷贝构造函数。我哪里出错?
非常感谢帮助!
我的代码如下:
#include<iostream>
#include<string>
#include<stdlib.h> // for c style exit
using namespace std;
class matrix
{
private:
double* mdata;
int rows, columns;
public:
// Declare the << operator as a friend of the class.
friend ostream& operator<<(ostream&, const matrix&);
// Define the default constructor.
matrix() : mdata(0), rows(0), columns(0) {}
// Define the parameterized constructor.
matrix(int numberOfRows, int numberOfColumns) : mdata(new double[numberOfColumns*numberOfColumns]), rows(numberOfRows), columns(numberOfColumns) {
for (int i{ 0 }; i <= numberOfColumns*numberOfRows; i++){
mdata[i] = 0;
}
}
// Define the copy constructor.
matrix(const matrix& matr) {
cout << endl << "Reached copy constructor." << endl;
rows = matr.getrows();
columns = matr.getcols();
mdata = 0;
if (matr.mdata){
mdata = new double[(matr.getrows())*(matr.getcols())];
for (int i{ 0 }; i <= rows*columns ; i++){
mdata[i] = matr.mdata[i];
cout << mdata[i] << endl;
}
}
else {
mdata = 0;
}
}
// Define the destructor.
~matrix() {
cout << "Destructor called on " << endl << *this;
delete[] mdata;
}
// Return the number of rows.
int getrows() const { return rows; }
// Return the number of columns.
int getcols() const { return columns; }
// Return the position in the array of element (m,n).
int index(int m, int n) const {
if (m>0 && m <= rows && n>0 && n <= columns) return (n - 1) + (m - 1)*columns;
else {
cout << "Error: out of range. Press any key to exit." << endl;
// For holding the program before exit.
exit(1);
}
}
// Allows array elements to be accessed with bracket notation.
double& operator()(int m, int n) const { return mdata[index(m, n)]; }
matrix& operator+(const matrix&) const;
// A function for edditing matrix elements
void setValue(int m, int n, double value) {
mdata[index(m, n)] = value;
}
};
// Define the << operator for printing matrices. This is a friend of the class.
ostream& operator<<(ostream &os, const matrix &mat){
// Deals with case of matrix with zero size.
if (mat.rows == 0 && mat.columns == 0){
cout << "This matrix is has no size." << endl;
}
// If the matrix has non-zero size,
else {
// For each row in the matrix,
for (int i{ 1 }; i <= mat.rows; i++) {
cout << "[";
// For each column in that row
for (int j{ 1 }; j <= mat.columns; j++){
// Print the element, followed by a comma.
if (j < mat.columns){
cout << mat(i, j) << ", ";
}
// Ensures no comma is placed after last elemet in row.
else{
cout << mat(i, j);
}
}
os << "]" << endl;
}
}
return os;
}
// The addition operator
matrix& matrix::operator+(const matrix& mat) const {
matrix result(rows, columns);
// If the matrix dimensions are different,
if (rows != mat.getrows() || columns != mat.getcols()){
cout << "Cannot add two matrices with differing dimensions." << endl;
string exitStr;
cout << "Enter any key to exit." << endl;
cin >> exitStr;
// Exit the program
exit(1);
}
// Otherwise, create a matrix with the appropriate dimensions
else{
// For each element in result
for (int k{ 1 }; k <= result.getrows(); ++k){
for (int l{ 1 }; l <= result.getcols(); ++l){
// That element is the sum of the corresponding elements in the two input matrices.
result.setValue(l,k,(mat(l, k) + (*this)(l, k)));
}
}
}
return result;
}
// Main program
int main()
{
// Demonstrate default constructor
matrix a1;
cout << "Matrix a1: ";
cout << a1;
// Demonstrate parameterized constructor
const int m(2), n(2);
matrix a2(m, n);
// Set values for a2
a2.setValue(1, 1, 1);
a2.setValue(2, 2, 1);
a2.setValue(1, 2, 1);
a2.setValue(2, 1, 1);
// Print matrix a2
cout << "a2 = " << endl << a2 << endl;
// Demonstrate parameterized constructor
matrix a3(m, n);
// Set values for a3
a3.setValue(1, 1, 2);
a3.setValue(2, 2, 2);
a3.setValue(1, 2, 2);
a3.setValue(2, 1, 2);
// Print matrix a3
cout << "a3 = " << endl << a3 << endl;
// Create matrix a4
matrix a4(a2 + a3);
cout << endl << "The sum of a2 and a3 is: " << endl << a4;
// For halting the program.
int x;
cin >> x;
return 0;
}
答案 0 :(得分:0)
for(int i {0}; i&lt; = numberOfColumns * numberOfRows; i ++)
应该是
for(int i {0}; i&lt; numberOfColumns * numberOfRows; i ++)
你正在循环N + 1而不是N