gpt4 book ai didi

c++ - 操作符不匹配* =

转载 作者:行者123 更新时间:2023-12-01 15:05:25 26 4
gpt4 key购买 nike

我一直在学校的一个项目中工作,该项目给了我一些编译问题,想知道我是否可以再看看我做错的事情,我一直遇到的错误在我的测试的第47行中文件说运算符* =不匹配,这让我很困惑。我将包括.h(标题)、. cc(实现)和测试文件。

#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>

using namespace std;
class Matrix {
public:
// Construction
Matrix(int, int, double);
// Copy construction
Matrix(const Matrix& m);
// Destructor
~Matrix();
// Index operators
const double& operator()(int i, int j) const; //to work on const objects
double& operator()(int i, int j);
// Copy assignment operator
Matrix& operator=(const Matrix& m);
// Compound arithmetic operators
Matrix& operator+=(const Matrix& x);
Matrix& operator-=(const Matrix& x);
Matrix& operator*=(const Matrix& m);
Matrix& operator*=(double d); // scalar multiplication
// Output
void print(ostream& sout) const; //display the matrix onto output stream
// sout neatly
int ncols() const; //return the number of columns
int nrows() const; // return the number of rows
private:
int rows; // number of rows
int cols; // number of columns
double **element; //dynamic array to hold data
};
// Arithmetic operators are not members
Matrix operator+(const Matrix& l, const Matrix&r); // return l+r
Matrix operator-(const Matrix& l, const Matrix&r); // return l-r
Matrix operator*(const Matrix& l, const Matrix&r); // return l*r
Matrix operator*(double d, const Matrix& r); // return d*l
Matrix operator*(const Matrix& m, double d); // return l*d
// Overloaded stream insertion operator
ostream& operator<<(ostream& out, const Matrix& x);
#endif

现在这是我的.cc实现文件
#include<iostream>
#include<cstdlib>
#include<stdexcept>
#include<cassert>
#include<iomanip>
#include"Matrix.h"
using namespace std;

Matrix::Matrix(int r=0, int c=0, double d=0.0) // default constructor to initialize everything to zero

{
rows=r;
cols=c;
element=new double*[r];
for(int i=0;i<r;++i){
element[i]=new double[c];}
for(int i=0; i<r; ++i){
for(int j=0; j<c; ++j){
element[i][j]=d;
}
}
}
Matrix::Matrix(const Matrix& m)
{

rows=m.rows;
cols=m.cols;
element=new double*[rows];
for(int i=0;i<rows;++i)
element[i]= new double[cols];

for(int i=0; i<rows;++i)
for(int j=0; j<cols;++j)
element[i][j]=m.element[i][j];

}



Matrix::~Matrix()
{
for (int i=0;i<rows;i++){
delete [] element[i];
}

delete [] element;
}



const double& Matrix::operator() (int i, int j) const
{
return element[i][j];
}




double& Matrix::operator()(int i, int j)
{
return element[i][j];
}



Matrix& Matrix::operator=(const Matrix& m)
{

if(this==&m){
return *this;
}
else if(rows !=m.rows || cols !=m.cols)
{
delete [] element;
rows=m.rows;
cols=m.cols;
element= new double*[rows];
for(int i=0; i<rows;i++){
element[i]=new double[cols];
}
}

return *this;}




Matrix& Matrix::operator+=(const Matrix& x)
{

for(int i=0; i<rows; ++i){
for(int j=0; j<cols; ++j){
element[i][j]+=x.element[i][j];
}
}
return *this;
}


Matrix& Matrix::operator-=(const Matrix& x)
{

for(int i=0; i<rows; ++i){
for(int j=0; j<cols; ++j){
element[i][j]-=x.element[i][j];
}
}
return *this;

}


Matrix& Matrix::operator*=(const Matrix& m)
{
Matrix temp( rows, m.cols);
for(int i=0; i<temp.rows; ++i){
for(int j=0; j<temp.cols; ++j){
for(int k=0; k<cols; ++k){
temp.element[i][j]+=(element[i][k]* m.element[k][j]);
}
}
}

return(*this=temp);


}

Matrix& Matrix::operator*=(double d)
{

for(int i=0; i<rows; ++i){
for(int j=0; j<cols; ++j){
element[i][j] *= d;
}
}
return *this;
}


void Matrix::print(ostream& sout) const
{


for (int i = 0; i < rows; ++i) {
sout << element[i][0];
for (int j = 1; j < cols; ++j) {
sout << " " << element[i][j];
}
sout << endl;
}

}
int Matrix::ncols() const
{
return cols;
}


int Matrix::nrows() const
{
return rows;
}

Matrix operator+(const Matrix& l, const Matrix&r)
{
Matrix temp(l);
return(temp+=r);

}

Matrix operator-(const Matrix& l, const Matrix&r)
{
Matrix temp(l);
return (temp -=r);
}

Matrix operator*(const Matrix& l, const Matrix&r)
{
Matrix temp(l);
return (temp*=r);
}

Matrix operator*(double d, const Matrix&r)
{
return(r*d);
}

Matrix operator*(const Matrix& m, double d)
{
Matrix temp(m);
return(temp*=d);
}

ostream& operator<<(ostream& out, const Matrix& x)
{
x.print(out);
return out;
}

现在这是我的测试文件,我尝试在其中编译时出现错误:

test_matrix.cc:47:13:错误:“operator * =”不匹配(操作数类型为“std::ostream {aka std::basic_ostream}”和“int”)
cout <
#include<iostream>
#include"Matrix.h"
#include"Matrix.cc"
using namespace std;

int main()
{

Matrix m1(3,3,2.0);
Matrix m2(3,3,3.0);

cout<<"****Testing readMatrixData, and overoladed <<"<<endl;
cout<<"Matrix m1 is:"<<endl;
cout<<m1;

cout<<"Matrix m2 is:"<<endl;
cout<<m2;

cout<<"****Testing index subscript operator"<<endl;
cout<<"m1(2,1) is "<<m1(2,1)<<endl;
cout<<"Now Set m1(2,1) to -19"<<endl;
m1(2,1)=-19;
cout<<"m1 has "<<m1.nrows()<<"rows."<<endl;
cout<<"m1 has "<<m1.ncols()<<"columns."<<endl;
cout<<"m1 is now:"<<endl;
cout<<m1;

Matrix m3=m1+m2;
cout<<"Matrix m3 = m1 + m2:"<<endl;
cout<<m3;

Matrix m4=m1-m2;
cout<<"Matrix m4 = m1 - m2:"<<endl;
cout<<m4;

Matrix m5=m3*m4;
cout<<"Matrix m5 = m3 * m4:"<<endl;
cout<<m5;

Matrix m6=m3;
cout<<"After m6 =m3 matrix m6 is:"<<endl;
cout<<m6;


cout<<"****Testing scalar multiplication"<<endl;
cout<<"m3 *= 2 and m3 is now:"<<endl;
cout<<m3 *= 2.0;

Matrix m7 = m3 * 2.0;
cout<<"****Testing matrix times a constant"<<endl;
cout<<"m7 = m3 * 2 and m7 is:"<<endl;
cout<<m7;

Matrix m8 = 2.0 * m3;
cout<<"****Testing a scalar times a matrix"<<endl;
cout<<"m8 = 2 * m3 and m8 is:"<<endl;
cout<<m8;

Matrix m9(2,2,2.0);
cout<<"Matrix m9 is"<<endl;
cout<<m9;

Matrix m10(2,2,1.0);
cout<<"Matrix m10 is"<<endl;
cout<<m10;

cout<<"Testing *= with m9 *= m10. m9 is:"<<endl;
m9= m9*=m10;
cout<<m9;

cout<<"Testing += with m9 += m10. m9 is:"<<endl;
m9=m9+=m10;

cout<<"Testing -= with m9 -= m10. m9 is:"<<endl;
m9=m9-=m10;


return 0;
}

最佳答案

error: no match for ‘operator*=’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘int’)



运算符 *=precedence低于运算符 <<,因此, cout << m3 *= 2.0被评估为 (cout << m3) *= 2.0,这会产生错误。

您需要使用括号来获得所需的求值顺序。例如。:
cout << (m3 *= 2.0);

关于c++ - 操作符不匹配* =,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58981152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com