我一直在学校的一个项目中工作,该项目给了我一些编译问题,想知道我是否可以再看看我做错了什么,所以我一直遇到的错误在第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;
}
答案 0 :(得分:9)
error: no match for ‘operator*=’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘int’)
运算符*=
的{{3}}低于运算符<<
,因此,cout << m3 *= 2.0
的值为(cout << m3) *= 2.0
,会产生错误。
您需要使用括号来获取所需的求值顺序。例如:
cout << (m3 *= 2.0);