简体   繁体   English

运算符重载+动态矩阵

[英]Operator overloading + dynamic matrix

So I have decided to restart my program everything except the + operator is working now. 所以我决定重新启动我的程序,除了+运算符现在可以正常工作。 I get the read access violation exception 我收到读取访问冲突异常

Header File 头文件

#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

class MatrixType
{
private:
    int **matrix;
    int row;
    int col;
public:
    void setElement(int r, int c, int newvalue);
    void display();

    const MatrixType& operator=(const MatrixType& mat);
    MatrixType operator+(const MatrixType& mat) const;

    friend std::ostream & operator << (std::ostream & out, const MatrixType & mat);

    MatrixType(int r, int c);
    MatrixType(string fileName);
    MatrixType(const MatrixType& mat);
    MatrixType();
    ~MatrixType();
};

Implementation File 实施文件

#include "matrixType.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

void MatrixType::setElement(int r, int c, int newvalue)
{
    matrix[r][c] = newvalue;
}

void MatrixType::display()
{
    for (int i = 0; i < row; i++)
    {
        for (int r = 0; r < col; r++)
            cout << matrix[i][r] << " ";
        cout << endl;
    }
    cout << endl;
}

const MatrixType& MatrixType::operator=(const MatrixType& mat)
{
    if (row != mat.row)
    {
        cout << "The matrixes are not identical" << endl;
        return *this;
    }
    for (int i = 0; i < row; i++)
    {
        for (int r = 0; r < col; r++)
        {
            matrix[i][r] = mat.matrix[i][r];
        }
    }
    return *this;
}

MatrixType MatrixType::operator+(const MatrixType& mat) const
{
    MatrixType tempMatrix;
    if (row != mat.row)
    {
        cout << "The matrixes are not identical can not be added together" << endl;
        return *this;
    }
    else
    {
        for (int i = 0; i < mat.row; i++)
        {
            for (int r = 0; r < mat.col; r++)
            {
                tempMatrix.matrix[i][r] = mat.matrix[i][r] + matrix[i][r];
            }
        }
        return tempMatrix;
    }
}

std::ostream & operator << (std::ostream & out, const MatrixType & mat)
{
    for (int i = 0; i < mat.row; i++) {
        for (int j = 0; j < mat.col; j++) {
            out << mat.matrix[i][j] << "  ";
        }
        out << std::endl;
    }
    return out;
}

MatrixType::MatrixType(int r, int c)
{
    matrix = new int*[r];
    for (int i = 0; i < r; i++)
    {
        matrix[i] = new int[c];
    }
    row = r;  
    col = c;
    for (int i = 0; i < row; i++)
    {
        for (int s = 0; s < col; s++)
        {
            matrix[i][s] = 0;
        }
    }
}
MatrixType::MatrixType(string fileName)
{
    int r;
    int c;
    int z;
    ifstream myFile;
    myFile.open(fileName);
    myFile >> r;
    myFile >> c;
    matrix = new int*[r];
    for (int i = 0; i < r; i++)
    {
        matrix[i] = new int[c];
    }

    for (int i = 0; i < r; i++)
    {
        for (int s = 0; s < c; s++)
        {
            myFile >> z;
            matrix[i][s] = z;
        }
    }
    row = r;
    col = c;
    myFile.close();
}

MatrixType::MatrixType(const MatrixType& mat)
{
    row = mat.row;
    col = mat.col;
    matrix = new int*[row];
    for (int i = 0; i < row; i++)
    {
        matrix[i] = new int[col];
    }
    for (int i = 0; i < row; i++)
    {
        for (int s = 0; s < col; s++)
        {
            matrix[i][s] = mat.matrix[i][s];
        }
    }
}

MatrixType::MatrixType()
{
}

MatrixType::~MatrixType()
{
    for (int i = 0; i < row; i++)
    {
        delete[] matrix[i];
    }
    delete[] matrix;
}

Source Code 源代码

#include "MatrixType.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    MatrixType M1("matrixfile1.txt");
    MatrixType M2("matrixfile2.txt");
    MatrixType M3("matrixfile3.txt");

    cout << "Matrix 1:" << endl << M1 << endl << endl;
    cout << "Matrix 2:" << endl << M2 << endl << endl;
    cout << "Matrix 3:" << endl << M3 << endl << endl;

    MatrixType M4 = M1 + M3;

    return 0;
}

So basically the goal is to check to see if the matrixes are identical otherwise you should just see a print the matrixes are not the same size. 因此,基本上的目标是检查矩阵是否相同,否则,您应该只看到矩阵尺寸不同的打印件。 The matrixfile1,2,3 that look like 矩阵文件1,2,3看起来像

3 3 3 3

1 1 1 1 1 1

1 1 1 1 1 1

1 1 1 1 1 1

That show the dimensions of the array and their elements. 这显示了数组及其元素的尺寸。 My question is what I could do to make my operator function work better as it is currently not able to add the matrixs together yet. 我的问题是我该怎么做才能使我的运算符功能更好地工作,因为它目前尚无法将矩阵加在一起。 Everything else though once again works its just the operator that gets a read access violation. 尽管其他所有内容再次可以正常工作,但只有操作员遇到读取访问冲突。

MatrixType::MatrixType()
{
}

This doesn't initialize the members row , col , or matrix at all, so it's undefined behavior to attempt to use the current values of any of them. 这根本不会初始化成员rowcolmatrix ,因此尝试使用其中任何一个的当前值是未定义的行为。

Since you haven't provided a way to change the dimensions of a MatrixType at all, even using operator= , there doesn't seem to be any good use of a default constructor, and probably the MatrixType() default constructor shouldn't exist at all. 由于您根本没有提供一种更改MatrixType的尺寸的方法,即使使用operator= ,似乎也没有很好地使用默认构造函数,并且MatrixType()默认构造函数可能不应该存在完全没有

MatrixType MatrixType::operator+(const MatrixType& mat) const
{
    MatrixType tempMatrix;

Your operator+ starts out by initializing tempMatrix using the default constructor. 您的operator+首先使用默认构造函数初始化tempMatrix So the dimensions of tempMatrix are...? 所以tempMatrix的尺寸是...?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM