两个类似的错误
#include<iostream.h>
typedef struct
{
float *p;
int m;
int n;
}matrix;
class DoMatrix
{
private:
matrix MTX;
public:
float &operator[](const int &bar_num);
int Updata(const DoMatrix &up_mtx);
void test(const DoMatrix &up_mtx);
};
void DoMatrix::test(const DoMatrix &up_mtx)
{int i;
MTX.p[i]=up_mtx[i]; //这里是错的
}
int DoMatrix::Updata(const DoMatrix &up_mtx)
{
if(!((MTX.m==up_mtx.MTX.m)&&(MTX.n==up_mtx.MTX.n)))
{
cout<<"You must update the wrong matrix"<<endl;
return 0;
}
else
{
for(int i=0;i<MTX.m*MTX.n;i++)
{
MTX.p[i]=up_mtx[i]; //这里为什么错?
}
return 1;
}
}
float &DoMatrix::operator[](const int &bar_num)
{
return MTX.p[bar_num];
}
void main()
{
}
问题点数:20、回复次数:10Top
1 楼nsdcomputer()回复于 2006-12-01 17:58:06 得分 0
为什么没有人回答??Top
2 楼imob419()回复于 2006-12-01 18:07:52 得分 0
一个是float类型,一个是DoMatrix类型,不匹配Top
3 楼nsdcomputer()回复于 2006-12-01 18:15:31 得分 0
编译结果是这样的:
C:\Documents and Settings\lib\桌面\新建文件夹 (2)\新建 文本文档.cpp(20) : error C2678: binary '[' : no operator defined which takes a left-hand operand of type 'const class DoMatrix' (or there is no acceptable conversion)
C:\Documents and Settings\lib\桌面\新建文件夹 (2)\新建 文本文档.cpp(33) : error C2678: binary '[' : no operator defined which takes a left-hand operand of type 'const class DoMatrix' (or there is no acceptable conversion)Top
4 楼cruzeflute(星幻)回复于 2006-12-01 18:16:42 得分 0
up_mtx 改成up_mtx.MTX.p
Top
5 楼lovesnow1314(流浪)回复于 2006-12-01 18:29:47 得分 0
change all MTX.p[i]=up_mtx[i] to
====>
MTX.p[i]=up_mtx.MTX.p[i];Top
6 楼nsdcomputer()回复于 2006-12-01 18:38:24 得分 0
如果把up_mtx 改成up_mtx.MTX.p ,那"[]"的重载就没有意义了;
我的意思是要重载"[]"Top
7 楼aniude(重返荣耀)回复于 2006-12-01 19:06:03 得分 0
up_mtx[i];
==>
up_mtx.MTX.p[i]
int i;这里的i没有初始化Top
8 楼nsdcomputer()回复于 2006-12-01 19:30:22 得分 0
有谁能给出个满意的答案,谢谢了Top
9 楼taodm((不能收CSDN社区短信息,请莫浪费精力))回复于 2006-12-04 08:46:20 得分 8
加一个float operator[](const int &bar_num) const{
return MTX.p[bar_num];
}Top
10 楼OOPhaisky(异化$渴望成功~~)回复于 2006-12-04 12:39:21 得分 12
void DoMatrix::test(const DoMatrix &up_mtx)
{int i;
MTX.p[i]=up_mtx[i]; //这里是错的
}
------------------------------------------------------------------------------------
楼主注意,你的up_mtx是const的,所以通过up_mtx只能调用DoMatrix的const member function,而你定义的operator[]是non-const member function,因此出错。Top





