C++ C++;带指针的动态二维数组(矩阵)
各位: 我正在创建一个程序,能够创建矩阵,并在学校的课程中对其执行各种操作。它们要求我们用适当的矩阵运算重载运算符 我在以下功能方面遇到了困难:C++ C++;带指针的动态二维数组(矩阵),c++,pointers,matrix,operator-overloading,C++,Pointers,Matrix,Operator Overloading,各位: 我正在创建一个程序,能够创建矩阵,并在学校的课程中对其执行各种操作。它们要求我们用适当的矩阵运算重载运算符 我在以下功能方面遇到了困难: typedef double matrixType; using namespace std; class Matrix{ protected: int m,n; // m:row size n:column size matrixType **a; //Allows us to acces the a(ij) i,j posi
typedef double matrixType;
using namespace std;
class Matrix{
protected:
int m,n; // m:row size n:column size
matrixType **a; //Allows us to acces the a(ij) i,j position of the matrix
//==================================================
// (==Operator)Verifies if two given Matrices are equal
//==================================================
bool Matrix::operator==(const Matrix &B){
bool flag=false;
if(B.m ==m && B.n ==n){
for (int row=0; row<m; row++) {
for (int col=0; col<n; col++) {
if (B[row][col] != a[row][col]) {
flag=false;
}
}
}
flag= true;
}
else{
flag=false;
}
return flag;
}
类型“const Matrix”未提供下标运算符
注意:此代码部分省略了函数头、构造函数和其他函数
任何帮助都将不胜感激。
谢谢。根据您的实现,如果(B.a[row][col]!=a[row][col]),则应该是
顺便说一句:如果你打算实现你自己的矩阵类,你应该读一读
if (B.a[row][col] != a[row][col]) {
flag=false;
}
如果你想一想,你就不能
if (B.a[row][col] != a[row][col]) {
return false;
}
跳过标志变量Altogher?如果是,为什么,如果不是,为什么不是(奖励您奖励积分;)
如果您想真正执行B[]],您必须为您的类实现运算符[]:
matrixType* operator[](size_t index)
{
return a[index];
}
从boost::ublas::matrix
,boost::gil::view_type
,OpenCV::Mat
等中获得提示,并使用运算符(int,int)
作为索引,而不是下标运算符。它更容易实现、维护和调试 他可能还应该给它一个下标操作符。我之所以使用标志是因为我担心IF结构中只有返回语句。我确实尝试在没有标志的情况下执行此操作,但从IDE收到一条警告:“控件可能会到达非void函数的末尾”@edu222是的,您应该在末尾添加另一个return语句。所以你需要3:1。如果B.m!返回false=m&B.n=n2。如果if失败,则返回false(这也被称为早期输出,想象一个10^10x10^10矩阵和所有不必要的比较,当它们在索引0,0;处不同时)。在方法结束时返回true。嗨,谢谢你的建议,我一有时间就会检查它。目前,我将使用当前的实现完成我的家庭作业,因为我几乎已经准备好了。:)
matrixType* operator[](size_t index)
{
return a[index];
}