在类中超载运算符时返回错误

发布于 2025-01-30 06:50:38 字数 13170 浏览 3 评论 0原文

当将矩阵乘以向量时,返回了一个方形矩阵,尽管它是在过载正文中构建的。我检查了“ =”运算符和复制构造函数,我是否缺少某些东西,还是问题是其他问题? 将正方形矩阵乘以矩形矩阵(列)时,由于这个问题可能是什么? 主:

#include <iostream>
#include "/home/qapfuc/Documents/Nizhny-Novgorod-State-University/Fourth LAB/vectorLib/MyMatrix.h"
#include "/home/qapfuc/Documents/Nizhny-Novgorod-State-University/Fourth LAB/vectorLib/MyVector.h"

int main(){
    MyVector<int> b{2,2};
    MyMatrix<int> y(2,2);
    MyMatrix<int> yy(2,2);
    std::cin>>yy;
    std::cin>>y;
    std::cout<<yy<<std::endl;
    yy=yy*y;
    std::cout<<yy<<std::endl;
}

mymatrix.h

#pragma once
#include "MyVector.h"

template <class ClassName>
class MyMatrix :public MyVector<ClassName>{
protected:
        ClassName **ArrayMatrix;
        int ArraySize_x = 3;
        int ArraySize_y = 3;
public:
        //Конструкторы
        MyMatrix() {
            ArrayMatrix = new ClassName*[ArraySize_x];
            for (int i = 0; i < ArraySize_x; i++) {
                ArrayMatrix[i] = new ClassName[ArraySize_y];
            }
            for (int i = 0; i < ArraySize_x; i++){
                for (int j = 0; j < ArraySize_y; j++){
                    ArrayMatrix[i][j]=0;
                }
            }
        }
        MyMatrix(int x,int y){
            ArraySize_x=x;
            ArraySize_y=y;
            ArrayMatrix = new ClassName*[ArraySize_x];
            for (int i = 0; i < ArraySize_x; i++) {
                ArrayMatrix[i] = new ClassName[ArraySize_y];
            }
            for (int i = 0; i < ArraySize_x; i++){
                for (int j = 0; j < ArraySize_y; j++){
                    ArrayMatrix[i][j]=0;
                }
            }
        }
        MyMatrix(const MyMatrix<ClassName> &other){
            ArraySize_x=other.ArraySize_x;
            ArraySize_y=other.ArraySize_y;
            ArrayMatrix = new  ClassName*[ArraySize_x];
            for (int i = 0; i < ArraySize_x; i++) {
                ArrayMatrix[i] = new ClassName[ArraySize_y];
            }
            for (int i = 0; i < ArraySize_x; i++){
                for (int j = 0; j < ArraySize_y; j++){
                    ArrayMatrix[i][j]=other.ArrayMatrix[i][j];
                }
            }
        }
        ~MyMatrix(){
            for (int i = 0; i < ArraySize_x; i++) {
                delete [] ArrayMatrix[i];
            }
            delete [] ArrayMatrix;
        }

        //Операторы
        auto operator[](const int index){
            return ArrayMatrix[index];
        };
        MyMatrix operator = (MyMatrix<ClassName> other) {
            if ((ArraySize_x == other.ArraySize_x) && (ArraySize_y == other.ArraySize_y)) {
                for (int i = 0; i < ArraySize_x; i++) {
                    for (int j = 0; j < ArraySize_y; j++) {
                        ArrayMatrix[i][j] = other.ArrayMatrix[i][j];
                    }
                }
                return *this;
            } else{
                MyMatrix<ClassName> temp(other.ArraySize_x,other.ArraySize_y);
                for (int i = 0; i < other.ArraySize_x; i++) {
                    for (int j = 0; j < other.ArraySize_y; j++) {
                        temp.ArrayMatrix[i][j] = other.ArrayMatrix[i][j];
                    }
                }
                return temp;
            }
        };
        MyMatrix operator+(MyMatrix<ClassName>& other){
            if ((ArraySize_x == other.ArraySize_x) && (ArraySize_y == other.ArraySize_y)) {
                MyMatrix<ClassName> temp(ArraySize_x, ArraySize_y);
                for (int i = 0; i < ArraySize_x; i++) {
                    for (int j = 0; j < ArraySize_y; j++) {
                        temp.ArrayMatrix[i][j] = ArrayMatrix[i][j] + other.ArrayMatrix[i][j];
                    }
                }
                return temp;
            }else{
                std::cout<<"The number of rows does not match the number of columns in the matrices. Check the matrices."<<std::endl;
                abort();
            }
        };
        MyMatrix operator-(MyMatrix<ClassName>& other){
            if ((ArraySize_x == other.ArraySize_x) && (ArraySize_y == other.ArraySize_y)) {
                MyMatrix<ClassName> temp(ArraySize_x, ArraySize_y);
                for (int i = 0; i < ArraySize_x; i++) {
                    for (int j = 0; j < ArraySize_y; j++) {
                        temp.ArrayMatrix[i][j] = ArrayMatrix[i][j] - other.ArrayMatrix[i][j];
                    }
                }
                return temp;
            }else{
                std::cout<<"The number of rows does not match the number of columns in the matrices. Check the matrices."<<std::endl;
                abort();
            }
        };
        MyMatrix operator*(MyMatrix<ClassName>& other){
            if(ArraySize_y==other.ArraySize_x) {
                MyMatrix<ClassName> temp(ArraySize_x, other.ArraySize_y);
                for (int i = 0; i < ArraySize_x; i++) {
                    for (int j = 0; j < other.ArraySize_x; j++) {
                        for (int k = 0; k < ArraySize_y; k++) {
                            temp.ArrayMatrix[i][j] += ArrayMatrix[i][k] * other.ArrayMatrix[k][j];
                        }
                    }
                }
                return temp;
            }
            else{
                std::cout<<"The number of rows does not match the number of columns in the matrices. Check the matrices."<<std::endl;
                abort();
            }
        };





        MyMatrix operator*(MyVector<ClassName>& other){
            if(ArraySize_y==other.GetSize()) {
                MyMatrix<ClassName> temp(other.GetSize(),1);
                for (int i = 0; i < temp.ArraySize_x; i++) {
                    for (int j = 0; j < 1; j++) {
                        for (int k = 0; k < other.GetSize(); k++) {
                            temp.ArrayMatrix[i][j] += ArrayMatrix[i][k] * other.GetArrayVector(k);
                        }
                    }
                }
                std::cout<<temp<<std::endl;
                return temp;
            }
            else{
                std::cout<<"The number of rows does not match the number of columns in the matrices. Check the matrices."<<std::endl;
                abort();
            }
        };





        friend std::ostream& operator<< (std::ostream& os, MyMatrix<ClassName> &Input){
            for( int i=0; i<Input.ArraySize_x;i++){
                for( int j=0; j<Input.ArraySize_y;j++) {
                    os << Input.ArrayMatrix[i][j]<<" ";
                }
                os<<std::endl;
            }
            return os;
        };
        friend std::istream& operator>>(std::istream &in, MyMatrix<ClassName>& Output){
            for( int i=0; i<Output.ArraySize_x;i++){
                for( int j=0; j<Output.ArraySize_y;j++) {
                    in >> Output.ArrayMatrix[i][j];
                }
            }
            return in;
        };

};
template <class ClassName>
bool operator==(MyMatrix<ClassName> &First,MyMatrix<ClassName> &Second){
    if((First.ArraySize_x!=Second.ArraySize_x) || (First.ArraySize_y!=Second.ArraySize_y)){
        return false;
    }
    for( int i=0;i<First.ArraySize_x;i++){
        for( int j=0;j<First.ArraySize_y;j++) {
            if (First.ArrayMatrix[i][j] != Second.ArrayMatrix[i][j]) {
                return false;
            }
        }
    }
    return true;
}
template <class ClassName>
bool operator!=(MyMatrix<ClassName> &First,MyMatrix<ClassName> &Second){
    return not(First==Second);
}

myVector.h

#pragma once
#include "MyMatrix.h"

template <class ClassName>
class MyVector{
protected:
        ClassName* ArrayVector;
        int ArraySize = 3;
public:
        ClassName GetArrayVector(int index){
            return ArrayVector[index];
        }
        int GetSize(){
            return ArraySize;
        }
        //Конструкторы
        MyVector(){
            ArrayVector = new ClassName[ArraySize];
            for( int i=0; i<ArraySize; i++){
                ArrayVector[i]=0;
            }
        }
        MyVector(int Size){
            ArraySize=Size;
            ArrayVector = new ClassName[ArraySize];
            for( int i=0; i<ArraySize; i++){
                ArrayVector[i]=0;
            }
        }
        MyVector(const std::initializer_list<ClassName> &list):MyVector(list.size()){
            int count = 0;
            for (auto &element : list)
            {
                ArrayVector[count] = element;
                ++count;
            }
        }
        MyVector(const MyVector<ClassName> &other){
            ArraySize=other.ArraySize;
            ArrayVector = new  ClassName[ArraySize];
            for( int i=0;i<ArraySize;i++){
                ArrayVector[i]=other.ArrayVector[i];
            }
        }
        ~MyVector(){
                delete [] ArrayVector;
        }

        //Операторы
        int& operator[](const int index){
            return ArrayVector[index];
        };
        MyVector operator=(MyVector<ClassName> other){
            if (ArraySize==other.ArraySize) {
                for (int i = 0; i < ArraySize; i++) {
                    ArrayVector[i] = other.ArrayVector[i];
                }
                return *this;
            } else {
                std::cout<<"The number of elements is not correct. Check the vector."<<std::endl;
                abort();
            }
        };
        MyVector operator+(MyVector<ClassName> other){
            if (ArraySize==other.ArraySize) {
                MyVector<ClassName> temp(ArraySize);
                for (int i = 0; i < ArraySize; i++) {
                    temp.ArrayVector[i] = ArrayVector[i] + other.ArrayVector[i];
                }
                return temp;
            }else{
                std::cout<<"The number of elements is not correct. Check the vector."<<std::endl;
                abort();
            }
        };
        MyVector operator-(MyVector<ClassName> other){
            if (ArraySize==other.ArraySize) {
                MyVector<ClassName> temp(ArraySize);
                for (int i = 0; i < ArraySize; i++) {
                    temp.ArrayVector[i] = ArrayVector[i] - other.ArrayVector[i];
                }
                return temp;
            } else {
                std::cout<<"The number of elements is not correct. Check the vector."<<std::endl;
                abort();
            }
        };
        MyVector operator*(MyVector<ClassName> other){
            if (ArraySize==other.ArraySize) {
                MyVector<ClassName> temp(ArraySize);
                for (int i = 0; i < ArraySize; i++) {
                    temp.ArrayVector[i] = ArrayVector[i] * other.ArrayVector[i];
                }
                return temp;
            } else {
                std::cout<<"The number of elements is not correct. Check the vector."<<std::endl;
                abort();
            }
        };
        MyVector operator/(MyVector<ClassName> other){
            if (ArraySize==other.ArraySize) {
                MyVector<ClassName> temp(ArraySize);
                for (int i = 0; i < ArraySize; i++) {
                    temp.ArrayVector[i] = (ArrayVector[i])/(other.ArrayVector[i]);
                }
                return temp;
            } else {
                std::cout<<"The number of elements is not correct. Check the vector."<<std::endl;
                abort();
            }
        };

        friend std::ostream& operator<< (std::ostream& os, MyVector<ClassName> &Input){
            for( int i=0; i<Input.ArraySize;i++){
                os<<Input.ArrayVector[i]<<std::endl;
            }
            return os;
        };

        friend std::istream& operator>>(std::istream &in, MyVector<ClassName>& Output){
            for( int i=0; i<Output.ArraySize;i++){
                in>>Output.ArrayVector[i];
            }
            return in;
        };
};
template <class ClassName>
bool operator==(MyVector<ClassName> &First,MyVector<ClassName> &Second){
    if(First.ArraySize!=Second.ArraySize){
        return false;
    }
    for( int i=0;i<First.ArraySize;i++){
        if(First.ArrayVector[i]!=Second.ArrayVector[i]){
            return false;
        }
    }
    return true;
}
template <class ClassName>
bool operator!=(MyVector<ClassName> &First,MyVector<ClassName> &Second){
    return not(First==Second);
}

When multiplying a matrix by a vector, a square matrix is ​​returned, although it is built rectangular in the body of the overload. I checked the "=" operator and the copy constructor, am I missing something or is the problem something else?
There is also an error when multiplying a square matrix by a rectangular one (column) because of what this problem can be?
main:

#include <iostream>
#include "/home/qapfuc/Documents/Nizhny-Novgorod-State-University/Fourth LAB/vectorLib/MyMatrix.h"
#include "/home/qapfuc/Documents/Nizhny-Novgorod-State-University/Fourth LAB/vectorLib/MyVector.h"

int main(){
    MyVector<int> b{2,2};
    MyMatrix<int> y(2,2);
    MyMatrix<int> yy(2,2);
    std::cin>>yy;
    std::cin>>y;
    std::cout<<yy<<std::endl;
    yy=yy*y;
    std::cout<<yy<<std::endl;
}

MyMatrix.h

#pragma once
#include "MyVector.h"

template <class ClassName>
class MyMatrix :public MyVector<ClassName>{
protected:
        ClassName **ArrayMatrix;
        int ArraySize_x = 3;
        int ArraySize_y = 3;
public:
        //Конструкторы
        MyMatrix() {
            ArrayMatrix = new ClassName*[ArraySize_x];
            for (int i = 0; i < ArraySize_x; i++) {
                ArrayMatrix[i] = new ClassName[ArraySize_y];
            }
            for (int i = 0; i < ArraySize_x; i++){
                for (int j = 0; j < ArraySize_y; j++){
                    ArrayMatrix[i][j]=0;
                }
            }
        }
        MyMatrix(int x,int y){
            ArraySize_x=x;
            ArraySize_y=y;
            ArrayMatrix = new ClassName*[ArraySize_x];
            for (int i = 0; i < ArraySize_x; i++) {
                ArrayMatrix[i] = new ClassName[ArraySize_y];
            }
            for (int i = 0; i < ArraySize_x; i++){
                for (int j = 0; j < ArraySize_y; j++){
                    ArrayMatrix[i][j]=0;
                }
            }
        }
        MyMatrix(const MyMatrix<ClassName> &other){
            ArraySize_x=other.ArraySize_x;
            ArraySize_y=other.ArraySize_y;
            ArrayMatrix = new  ClassName*[ArraySize_x];
            for (int i = 0; i < ArraySize_x; i++) {
                ArrayMatrix[i] = new ClassName[ArraySize_y];
            }
            for (int i = 0; i < ArraySize_x; i++){
                for (int j = 0; j < ArraySize_y; j++){
                    ArrayMatrix[i][j]=other.ArrayMatrix[i][j];
                }
            }
        }
        ~MyMatrix(){
            for (int i = 0; i < ArraySize_x; i++) {
                delete [] ArrayMatrix[i];
            }
            delete [] ArrayMatrix;
        }

        //Операторы
        auto operator[](const int index){
            return ArrayMatrix[index];
        };
        MyMatrix operator = (MyMatrix<ClassName> other) {
            if ((ArraySize_x == other.ArraySize_x) && (ArraySize_y == other.ArraySize_y)) {
                for (int i = 0; i < ArraySize_x; i++) {
                    for (int j = 0; j < ArraySize_y; j++) {
                        ArrayMatrix[i][j] = other.ArrayMatrix[i][j];
                    }
                }
                return *this;
            } else{
                MyMatrix<ClassName> temp(other.ArraySize_x,other.ArraySize_y);
                for (int i = 0; i < other.ArraySize_x; i++) {
                    for (int j = 0; j < other.ArraySize_y; j++) {
                        temp.ArrayMatrix[i][j] = other.ArrayMatrix[i][j];
                    }
                }
                return temp;
            }
        };
        MyMatrix operator+(MyMatrix<ClassName>& other){
            if ((ArraySize_x == other.ArraySize_x) && (ArraySize_y == other.ArraySize_y)) {
                MyMatrix<ClassName> temp(ArraySize_x, ArraySize_y);
                for (int i = 0; i < ArraySize_x; i++) {
                    for (int j = 0; j < ArraySize_y; j++) {
                        temp.ArrayMatrix[i][j] = ArrayMatrix[i][j] + other.ArrayMatrix[i][j];
                    }
                }
                return temp;
            }else{
                std::cout<<"The number of rows does not match the number of columns in the matrices. Check the matrices."<<std::endl;
                abort();
            }
        };
        MyMatrix operator-(MyMatrix<ClassName>& other){
            if ((ArraySize_x == other.ArraySize_x) && (ArraySize_y == other.ArraySize_y)) {
                MyMatrix<ClassName> temp(ArraySize_x, ArraySize_y);
                for (int i = 0; i < ArraySize_x; i++) {
                    for (int j = 0; j < ArraySize_y; j++) {
                        temp.ArrayMatrix[i][j] = ArrayMatrix[i][j] - other.ArrayMatrix[i][j];
                    }
                }
                return temp;
            }else{
                std::cout<<"The number of rows does not match the number of columns in the matrices. Check the matrices."<<std::endl;
                abort();
            }
        };
        MyMatrix operator*(MyMatrix<ClassName>& other){
            if(ArraySize_y==other.ArraySize_x) {
                MyMatrix<ClassName> temp(ArraySize_x, other.ArraySize_y);
                for (int i = 0; i < ArraySize_x; i++) {
                    for (int j = 0; j < other.ArraySize_x; j++) {
                        for (int k = 0; k < ArraySize_y; k++) {
                            temp.ArrayMatrix[i][j] += ArrayMatrix[i][k] * other.ArrayMatrix[k][j];
                        }
                    }
                }
                return temp;
            }
            else{
                std::cout<<"The number of rows does not match the number of columns in the matrices. Check the matrices."<<std::endl;
                abort();
            }
        };





        MyMatrix operator*(MyVector<ClassName>& other){
            if(ArraySize_y==other.GetSize()) {
                MyMatrix<ClassName> temp(other.GetSize(),1);
                for (int i = 0; i < temp.ArraySize_x; i++) {
                    for (int j = 0; j < 1; j++) {
                        for (int k = 0; k < other.GetSize(); k++) {
                            temp.ArrayMatrix[i][j] += ArrayMatrix[i][k] * other.GetArrayVector(k);
                        }
                    }
                }
                std::cout<<temp<<std::endl;
                return temp;
            }
            else{
                std::cout<<"The number of rows does not match the number of columns in the matrices. Check the matrices."<<std::endl;
                abort();
            }
        };





        friend std::ostream& operator<< (std::ostream& os, MyMatrix<ClassName> &Input){
            for( int i=0; i<Input.ArraySize_x;i++){
                for( int j=0; j<Input.ArraySize_y;j++) {
                    os << Input.ArrayMatrix[i][j]<<" ";
                }
                os<<std::endl;
            }
            return os;
        };
        friend std::istream& operator>>(std::istream &in, MyMatrix<ClassName>& Output){
            for( int i=0; i<Output.ArraySize_x;i++){
                for( int j=0; j<Output.ArraySize_y;j++) {
                    in >> Output.ArrayMatrix[i][j];
                }
            }
            return in;
        };

};
template <class ClassName>
bool operator==(MyMatrix<ClassName> &First,MyMatrix<ClassName> &Second){
    if((First.ArraySize_x!=Second.ArraySize_x) || (First.ArraySize_y!=Second.ArraySize_y)){
        return false;
    }
    for( int i=0;i<First.ArraySize_x;i++){
        for( int j=0;j<First.ArraySize_y;j++) {
            if (First.ArrayMatrix[i][j] != Second.ArrayMatrix[i][j]) {
                return false;
            }
        }
    }
    return true;
}
template <class ClassName>
bool operator!=(MyMatrix<ClassName> &First,MyMatrix<ClassName> &Second){
    return not(First==Second);
}

MyVector.h

#pragma once
#include "MyMatrix.h"

template <class ClassName>
class MyVector{
protected:
        ClassName* ArrayVector;
        int ArraySize = 3;
public:
        ClassName GetArrayVector(int index){
            return ArrayVector[index];
        }
        int GetSize(){
            return ArraySize;
        }
        //Конструкторы
        MyVector(){
            ArrayVector = new ClassName[ArraySize];
            for( int i=0; i<ArraySize; i++){
                ArrayVector[i]=0;
            }
        }
        MyVector(int Size){
            ArraySize=Size;
            ArrayVector = new ClassName[ArraySize];
            for( int i=0; i<ArraySize; i++){
                ArrayVector[i]=0;
            }
        }
        MyVector(const std::initializer_list<ClassName> &list):MyVector(list.size()){
            int count = 0;
            for (auto &element : list)
            {
                ArrayVector[count] = element;
                ++count;
            }
        }
        MyVector(const MyVector<ClassName> &other){
            ArraySize=other.ArraySize;
            ArrayVector = new  ClassName[ArraySize];
            for( int i=0;i<ArraySize;i++){
                ArrayVector[i]=other.ArrayVector[i];
            }
        }
        ~MyVector(){
                delete [] ArrayVector;
        }

        //Операторы
        int& operator[](const int index){
            return ArrayVector[index];
        };
        MyVector operator=(MyVector<ClassName> other){
            if (ArraySize==other.ArraySize) {
                for (int i = 0; i < ArraySize; i++) {
                    ArrayVector[i] = other.ArrayVector[i];
                }
                return *this;
            } else {
                std::cout<<"The number of elements is not correct. Check the vector."<<std::endl;
                abort();
            }
        };
        MyVector operator+(MyVector<ClassName> other){
            if (ArraySize==other.ArraySize) {
                MyVector<ClassName> temp(ArraySize);
                for (int i = 0; i < ArraySize; i++) {
                    temp.ArrayVector[i] = ArrayVector[i] + other.ArrayVector[i];
                }
                return temp;
            }else{
                std::cout<<"The number of elements is not correct. Check the vector."<<std::endl;
                abort();
            }
        };
        MyVector operator-(MyVector<ClassName> other){
            if (ArraySize==other.ArraySize) {
                MyVector<ClassName> temp(ArraySize);
                for (int i = 0; i < ArraySize; i++) {
                    temp.ArrayVector[i] = ArrayVector[i] - other.ArrayVector[i];
                }
                return temp;
            } else {
                std::cout<<"The number of elements is not correct. Check the vector."<<std::endl;
                abort();
            }
        };
        MyVector operator*(MyVector<ClassName> other){
            if (ArraySize==other.ArraySize) {
                MyVector<ClassName> temp(ArraySize);
                for (int i = 0; i < ArraySize; i++) {
                    temp.ArrayVector[i] = ArrayVector[i] * other.ArrayVector[i];
                }
                return temp;
            } else {
                std::cout<<"The number of elements is not correct. Check the vector."<<std::endl;
                abort();
            }
        };
        MyVector operator/(MyVector<ClassName> other){
            if (ArraySize==other.ArraySize) {
                MyVector<ClassName> temp(ArraySize);
                for (int i = 0; i < ArraySize; i++) {
                    temp.ArrayVector[i] = (ArrayVector[i])/(other.ArrayVector[i]);
                }
                return temp;
            } else {
                std::cout<<"The number of elements is not correct. Check the vector."<<std::endl;
                abort();
            }
        };

        friend std::ostream& operator<< (std::ostream& os, MyVector<ClassName> &Input){
            for( int i=0; i<Input.ArraySize;i++){
                os<<Input.ArrayVector[i]<<std::endl;
            }
            return os;
        };

        friend std::istream& operator>>(std::istream &in, MyVector<ClassName>& Output){
            for( int i=0; i<Output.ArraySize;i++){
                in>>Output.ArrayVector[i];
            }
            return in;
        };
};
template <class ClassName>
bool operator==(MyVector<ClassName> &First,MyVector<ClassName> &Second){
    if(First.ArraySize!=Second.ArraySize){
        return false;
    }
    for( int i=0;i<First.ArraySize;i++){
        if(First.ArrayVector[i]!=Second.ArrayVector[i]){
            return false;
        }
    }
    return true;
}
template <class ClassName>
bool operator!=(MyVector<ClassName> &First,MyVector<ClassName> &Second){
    return not(First==Second);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文