Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 有重载矩阵乘法的问题“*&引用;操作人员_C++_Oop_C++11_Matrix - Fatal编程技术网

C++ 有重载矩阵乘法的问题“*&引用;操作人员

C++ 有重载矩阵乘法的问题“*&引用;操作人员,c++,oop,c++11,matrix,C++,Oop,C++11,Matrix,我想为我冗长的问题道歉,这只是我第二次使用这个网站。在不久的将来,我将确保以有序的方式综合我的问题 我正在编写一个包含重载乘法的n×m矩阵类。目前,只有当第一个输入矩阵的列与第二个矩阵的行匹配时,代码才能用于n×n和n×m矩阵的乘法,反之亦然。例如: a1 a2 * b1 b2 这是可行的(很抱歉,我没有足够的声誉点数来发布图片,但可以将其视为1x2矩阵*2x1矩阵) 但这种类型的输入不起作用 my矩阵类由以下公式给出: #include<iostream> #in

我想为我冗长的问题道歉,这只是我第二次使用这个网站。在不久的将来,我将确保以有序的方式综合我的问题

我正在编写一个包含重载乘法的n×m矩阵类。目前,只有当第一个输入矩阵的列与第二个矩阵的行匹配时,代码才能用于n×n和n×m矩阵的乘法,反之亦然。例如:

a1 a2 * b1
        b2
这是可行的(很抱歉,我没有足够的声誉点数来发布图片,但可以将其视为1x2矩阵*2x1矩阵)

但这种类型的输入不起作用

my矩阵类由以下公式给出:

#include<iostream>
#include <stdlib.h>
using namespace std;
class dymatrix
{
    friend ostream & operator << (ostream &os, const dymatrix &om);
    friend istream & operator >> (istream &is, dymatrix &om);
private:
    int rows;
    int columns;
    double *matrix;
public:
    dymatrix(){cout<<"Default constructor called"<<endl; columns = 0; rows=0; matrix=0;}
    ~dymatrix(){cout<<"Destructor called"<<endl;delete[] matrix;}  
    dymatrix(int inrows, int incolumns) //Parametrised objects, matrix database is defined here to store data.
    {
        rows = inrows;
        columns = incolumns;
        matrix = new double [inrows*incolumns];
        for (int i=0; i<inrows*incolumns; i++) 
        { 
            matrix[i]=0;
        }
    }
    int lengthr() const {return rows;}  //Returns number of rows.
    int lengthc() const {return columns;}   //Return number of columns.
    dymatrix &operator=( dymatrix &arr); //This is the assignment member function.
    dymatrix(dymatrix&);    //This is the deep copy member function.
    dymatrix operator*(const dymatrix& arr)const ;  //Overloading "*" operator.

    double & operator[](const int n)    //Overloading "[]" operator, this is important !!!
    {
        if(n<0 || n>=rows*columns)
        {
            cout<<"Error: trying to access matrix element out of bounds"<<endl;
            exit(1);
        }
    return matrix[n];
    }
    int index(int j, int i) const //This member function returns the position of each index.
    {
        if (j > 0 && j <=rows && i > 0 && i <=columns)
        {
        //cout<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl;
            return (i-1)+(j-1)*columns;
        }
        else {cout<<"Error, out of range"<<endl<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl; exit (1);}
    }
    double & operator()(int j, int i) const {return matrix[index(j,i)];} //The operator () returns the position of j and i in 1D array.
    }; //Class End.
#包括
#包括
使用名称空间std;
类并矢矩阵
{
friend ostream&operator>(istream&is、dymatrix&om);
私人:
int行;
int列;
双*矩阵;
公众:

dymatrix(){cout在乘法运算符代码中,此行错误:

new_matrix.matrix[index(j,i)]= new_matrix.matrix[index(j,i)]+ matrix[index(j,k)]*arr(k,i);
问题是
索引(j,i)
实际上是
这个->索引(j,i)
,而不是
新的矩阵

您应该将其替换为以下内容:

new_matrix(j,i) += matrix[index(j,k)]*arr(k,i);

但真正的问题是:有没有什么好的理由让您实现自己的矩阵类,而不是使用像Armadillo这样经过战斗测试的矩阵库?

Fyi
dymatrix&operator=(dymatrix&arr)当赋值的右边是临时的时,它就在你的
resu_a3=a1*a2;
语句中,你会不高兴。除非你使用的编译器支持对temp的非标准非常量引用(例如:MS)这不会编译。例如,这不会用clang 3.5编译。谢谢,我使用的编译器工作正常。我已将其更改为
dymatrix&operator=(dymatrix arr);
但同样的问题仍然存在。哦,老兄……请帮我们大家一个忙,学会综合;如果仅仅阅读这个问题需要15分钟,你可能得不到任何帮助……我对此深表歉意,这是我第二次尝试在这个网站上提问。注意,我将学会综合我提出的任何问题不久的将来。@XiyangLiu:让
index
实际执行索引可能更有意义,而不仅仅是返回索引号,这样你就可以只执行
matrix.index(i,j)
而不是
matrix[matrix.index(i,j)]
。有些人会使函数调用操作符过载,所以你可以执行
matrix(i,j)
,但我不推荐这样做。@Matt:他有一个操作符()可以做到这一点:
(*this)(I,j)
=
矩阵[matrix.index(I,j)]
,这很好。在我看来,使用基于0的索引更有意义,因此循环和索引计算看起来“正常”…@MartinJ:我没有注意到。这就是当你发布太多代码时会发生的事情。。。
dymatrix dymatrix:: operator * (const dymatrix &arr) const //Overloading "*" operator.
{
    if (columns != arr.rows)
    {
        cerr<<"SIZE DO NOT MATCH, YOU FAIL"<<endl; exit(1);
    }
    dymatrix new_matrix(rows,arr.columns);
    for (int j = 1; j <= rows; j++)
    {
        for (int i = 1; i <= arr.columns; i++)
        {
            for (int k = 1; k <= columns; k++)
            {
                new_matrix.matrix[index(j,i)]= new_matrix.matrix[index(j,i)]+ matrix[index(j,k)]*arr(k,i);
                cout<<"new_matrix = "<<new_matrix<<endl<<endl;
            }
        }
    }
    return new_matrix;
}
int main()
{
    dymatrix a1;
    cin >> a1;  //Define the rows of the matrix
    cout << a1<<endl<<endl;
    dymatrix a2;
    cin >> a2;
    cout << a2<<endl<<endl;
    dymatrix resu_a3;
    resu_a3 = a1*a2;
    cout<<"Multiplication = "<<resu_a3<<endl;
    return 0;
}
#include<iostream>
#include <stdlib.h>
using namespace std;
class dymatrix
{
    friend ostream & operator << (ostream &os, const dymatrix &om);
    friend istream & operator >> (istream &is, dymatrix &om);
private:
    int rows;
    int columns;
    double *matrix;
public:
    dymatrix(){cout<<"Default constructor called"<<endl; columns = 0; rows=0; matrix=0;}
    ~dymatrix(){cout<<"Destructor called"<<endl;delete[] matrix;}  
    dymatrix(int inrows, int incolumns) //Parametrised objects, matrix database is defined here to store data.
    {
        rows = inrows;
        columns = incolumns;
        matrix = new double [inrows*incolumns];
        for (int i=0; i<inrows*incolumns; i++) 
        { 
            matrix[i]=0;
        }
    }
    int lengthr() const {return rows;}  //Returns number of rows.
    int lengthc() const {return columns;}   //Return number of columns.
    dymatrix &operator=( dymatrix &arr); //This is the assignment member function.
    dymatrix(dymatrix&);    //This is the deep copy member function.
    dymatrix operator*(const dymatrix& arr)const ;  //Overloading "*" operator.

    double & operator[](const int n)    //Overloading "[]" operator, this is important !!!
    {
        if(n<0 || n>=rows*columns)
        {
            cout<<"Error: trying to access matrix element out of bounds"<<endl;
            exit(1);
        }
    return matrix[n];
    }
    int index(int j, int i) const //This member function returns the position of each index.
    {
        if (j > 0 && j <=rows && i > 0 && i <=columns)
        {
        //cout<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl;
            return (i-1)+(j-1)*columns;
        }
        else {cout<<"Error, out of range"<<endl<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl; exit (1);}
    }
    double & operator()(int j, int i) const {return matrix[index(j,i)];} //The operator () returns the position of j and i in 1D array.
}; //Class End.
    dymatrix::dymatrix(dymatrix &arr)   //This is the copy constructor for deep copying.
    {
        matrix = 0; rows = arr.lengthr(); columns = arr.lengthc();
        if (rows*columns >0)
        {
            matrix = new double [rows*columns];
            for (int i =0; i < rows*columns; i++) 
            {
                matrix[i] = arr[i];
            }
        }
    }
    dymatrix & dymatrix ::operator = (dymatrix &arr)
    {
        if(&arr == this) return *this; // no self assignment
        delete[] matrix; matrix=0; rows=columns=0;
        rows=arr.lengthr();
        columns = arr.lengthc();
        if(rows*columns>0)
         {
            matrix=new double[rows*columns];
            // Copy values into new array
            for(int i=0;i<rows*columns;i++) matrix[i] = arr[i];
        }
    return *this; // Special pointer!!
    }
    istream & operator >> (istream &is, dymatrix &om)   //Overloading ">>" operator here to
    {
        cout<<"Please enter the number of rows you want"<<endl;
        is >> om.rows;  //Inputting number of rows.
        cout<<"Enter the number of columns you want"<<endl;
        is >> om.columns;   //Inputting number of columns.
        cout<<"Enter matrix"<<endl;
        om.matrix = new double [om.rows*om.columns];    //Making a dynamic array here to put the data in.
        for (int j = 1; j <= om.rows; j++)
        {
            for (int i = 1; i <= om.columns; i++)
            {
                is >> om.matrix[om.index(j,i)]; //Putting in the data into this dynamic array for each element.
            }
        }
        return is;
    }
    ostream & operator << (ostream &os, const dymatrix &om)  //To output the matrix in an standard matrix way
    {
        for(int j= 1; j<=om.rows; j++)
        {
            os<<endl<<endl;
            for (int i = 1; i <=om.columns;i++)
            {
                os << om.matrix[om.index(j,i)]<<"\t";   //Similar method used in istream.
            }
        }
        return os;
    }

    dymatrix dymatrix:: operator * (const dymatrix &arr) const //Overloading "*" operator.
    {
        if (columns != arr.rows)
        {
            cerr<<"SIZE DO NOT MATCH, YOU FAIL"<<endl; exit(1);
        }
        dymatrix new_matrix(rows,arr.columns);
        for (int j = 1; j <= rows; j++)
        {
            for (int i = 1; i <= arr.columns; i++)
            {
                for (int k = 1; k <= columns; k++)
                {
                    new_matrix.matrix[index(j,i)]= new_matrix.matrix[index(j,i)]+ matrix[index(j,k)]*arr(k,i);
                    cout<<"new_matrix = "<<new_matrix<<endl<<endl;
                    cout<<"k "<<k<<endl<<" arr.columns is "<<arr.columns<<endl<<"columns is "<<columns<<endl<<endl;
                }
            }
        }
        return new_matrix;
    }

int main()
{
    dymatrix a1;
    cin >> a1;  //Define the rows of the matrix
    cout << a1<<endl<<endl;
    dymatrix a2;
    cin >> a2;
    cout << a2<<endl<<endl;
    dymatrix resu_a3;
    resu_a3 = a1*a2;
    cout<<"Multiplication = "<<resu_a3<<endl;
    return 0;
}
new_matrix.matrix[index(j,i)]= new_matrix.matrix[index(j,i)]+ matrix[index(j,k)]*arr(k,i);
new_matrix(j,i) += matrix[index(j,k)]*arr(k,i);