CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
(图)邪恶的韩国UMPC 使用 Java 编写数据库应用新规范
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

两个类似的错误

楼主nsdcomputer()2006-12-01 17:38:56 在 C/C++ / C++ 语言 提问

#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

相关问题

关键词

得分解答快速导航

  • 帖主:nsdcomputer
  • taodm
  • OOPhaisky

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
惹火投票。。火热进行中...
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo