C++带指针的动态二维数组(矩阵)
大家:
我正在创建一个程序,能够创建矩阵并为学校的课程对其执行各种操作。它们要求我们使用适当的矩阵运算来使运算符过载。
我在使用以下函数时遇到了困难:
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;
}
Xcode 警告我在以下行中:
if (B[row][col] != a[row][col])
type 'const Matrix' 不提供下标运算符
注意:此代码部分中省略了函数头、构造函数和其他函数。
任何帮助将不胜感激。 谢谢。
everyone:
Im creating a program that is able to create Matrices and and perform various operations on them for a course at school. They require us to overload the operators with with the appropriate Matrix Operations.
I am having a hard time with the following function:
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;
}
Xcode warns me that at the following line:
if (B[row][col] != a[row][col])
type 'const Matrix' doesn't provide a subscript operator
Note: Function Headers,constructors and other functions have been omitted from this code portion.
Any help would be greatly appreciated.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鉴于您的实现,它应该是
if (Ba[row][col] != a[row][col])
顺便说一句:您应该阅读 此页面(如果您计划实现自己的矩阵类)。
Given your implementation, it should be
if (B.a[row][col] != a[row][col])
BTW: You should read this page if you plan to implement your own matrix class.
你想做的是
如果你想一想,你难道不能简单地
跳过标志变量吗?如果是,为什么,如果不是,为什么不(给你奖励积分;)。
如果你真的想做 B[][] 你必须为你的类实现operator[]:
What you meant to do was
And if you give it a moment of thought, couldn't you simply do
and skip the flag variable altogeher? If yes, why, if not, why not (awards you bonus points ;).
If you want to really do B[][] you would have to implement operator[] for your class:
从
boost::ublas::matrix
、boost::gil::view_type
、OpenCV::Mat
等获取提示并使用 < code>operator(int,int) 用于索引而不是下标运算符。它更容易实现、维护和调试。Take a cue from
boost::ublas::matrix
,boost::gil::view_type
,OpenCV::Mat
, etc and useoperator(int,int)
for indexes instead of the subscript operator. It is worlds easier to implement, maintain, and debug.