免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1550 | 回复: 6
打印 上一主题 下一主题

[C++] 函数重载求解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-04-28 07:05 |只看该作者 |倒序浏览
本帖最后由 ANONYMOUS419 于 2015-04-28 07:16 编辑
  1. #pragma once
  2. class Complex
  3. {
  4.         double real_, imag_;
  5. public:
  6.         Complex();
  7.         Complex(double, double);
  8.         ~Complex();
  9.         Complex & operator+=(const Complex &);
  10.         Complex & operator-=(const Complex &);
  11.         Complex & operator*=(const Complex &);
  12.         Complex & operator/=(const Complex &);
  13.         Complex & operator%=(const Complex &c2);
  14.         Complex & operator^=(const Complex &);
  15.         Complex & operator&=(const Complex &);
  16.         Complex & operator|=(const Complex &);
  17.         Complex & operator=(const Complex &);
  18.         bool operator>=(const Complex &);
  19.         bool operator<=(const Complex &);
  20.         bool operator==(const Complex &);
  21.         bool operator<(const Complex &);
  22.         bool operator>(const Complex &);
  23.         bool operator!=(const Complex &);
  24.         Complex & operator++();
  25.         Complex & operator--();
  26.         Complex operator++(int);
  27.         Complex operator--(int);
  28. };

  29. #include "Complex.h"



  30. Complex::Complex()
  31. {
  32.         real_ = imag_ = 0;
  33. }

  34. Complex::Complex(double r, double i):real_(r),imag_(i)
  35. {

  36. }

  37. Complex::~Complex()
  38. {
  39. }

  40. Complex & Complex::operator+=(const Complex & c2)
  41. {
  42.         real_ += c2.real_;
  43.         imag_ += c2.imag_;
  44.         return *this;
  45. }

  46. Complex & Complex::operator-=(const Complex & c2)
  47. {
  48.         real_ -= c2.real_;
  49.         imag_ -= c2.imag_;
  50.         return *this;
  51. }

  52. Complex & Complex::operator*=(const Complex & c2)
  53. {
  54.         real_ *= c2.real_;
  55.         imag_ *= c2.imag_;
  56.         return *this;
  57. }

  58. Complex & Complex::operator/=(const Complex & c2)
  59. {
  60.         if (c2.real_&&c2.imag_)
  61.         {
  62.                 real_ /= c2.real_;
  63.                 imag_ /= c2.imag_;        
  64.         }

  65.         return *this;
  66. }

  67. Complex & Complex::operator%=(const Complex & c2)
  68. {
  69.         int(real_) %= int(c2.real_);
  70.         int(imag_) %= int(c2.imag_);
  71.         return *this;
  72. }

  73. Complex & Complex::operator^=(const Complex &)
  74. {
  75.         // TODO: insert return statement here
  76. }

  77. Complex & Complex::operator&=(const Complex &)
  78. {
  79.         // TODO: insert return statement here
  80. }

  81. Complex & Complex::operator|=(const Complex &)
  82. {
  83.         // TODO: insert return statement here
  84. }

  85. Complex & Complex::operator=(const Complex &)
  86. {
  87.         // TODO: insert return statement here
  88. }

  89. bool Complex::operator>=(const Complex &)
  90. {
  91.         return false;
  92. }

  93. bool Complex::operator<=(const Complex &)
  94. {
  95.         return false;
  96. }

  97. bool Complex::operator==(const Complex &)
  98. {
  99.         return false;
  100. }

  101. bool Complex::operator<(const Complex &)
  102. {
  103.         return false;
  104. }

  105. bool Complex::operator>(const Complex &)
  106. {
  107.         return false;
  108. }

  109. bool Complex::operator!=(const Complex &)
  110. {
  111.         return false;
  112. }

  113. Complex & Complex::operator++()
  114. {
  115.         // TODO: insert return statement here
  116. }

  117. Complex & Complex::operator--()
  118. {
  119.         // TODO: insert return statement here
  120. }

  121. Complex Complex::operator++(int)
  122. {
  123.         return Complex();
  124. }

  125. Complex Complex::operator--(int)
  126. {
  127.         return Complex();
  128. }

复制代码
1>------ Rebuild All started: Project: HomeWork2, Configuration: Debug Win32 ------
1>  Source.cpp
1>  Complex.cpp
1>e:\c++语法与数据结构\c++语法与数据结构第8天\homework2\homework2\complex.cpp(53): error C2106: '%=': left operand must be l-value
1>e:\c++语法与数据结构\c++语法与数据结构第8天\homework2\homework2\complex.cpp(54): error C2106: '%=': left operand must be l-value
1>  Generating Code...
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
2 [报告]
发表于 2015-04-28 08:05 |只看该作者
左边你加  int() 干啥?

求职 : 机器学习
论坛徽章:
79
2015年亚洲杯纪念徽章
日期:2015-05-06 19:18:572015七夕节徽章
日期:2015-08-21 11:06:172015亚冠之阿尔纳斯尔
日期:2015-09-07 09:30:232015亚冠之萨济拖拉机
日期:2015-10-21 08:26:3915-16赛季CBA联赛之浙江
日期:2015-12-30 09:59:1815-16赛季CBA联赛之浙江
日期:2016-01-10 12:35:21技术图书徽章
日期:2016-01-15 11:07:2015-16赛季CBA联赛之新疆
日期:2016-02-24 13:46:0215-16赛季CBA联赛之吉林
日期:2016-06-26 01:07:172015-2016NBA季后赛纪念章
日期:2016-06-28 17:44:45黑曼巴
日期:2016-06-28 17:44:4515-16赛季CBA联赛之浙江
日期:2017-07-18 13:41:54
3 [报告]
发表于 2015-04-28 08:51 |只看该作者
这个是什么编辑器,效果挺好的啊

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
4 [报告]
发表于 2015-04-28 09:27 |只看该作者
zsszss0000 发表于 2015-04-28 08:51
这个是什么编辑器,效果挺好的啊


看最后的输出,应该是VS,修改了缺省的配色

论坛徽章:
36
子鼠
日期:2013-08-28 22:23:29黄金圣斗士
日期:2015-12-01 11:37:51程序设计版块每日发帖之星
日期:2015-12-14 06:20:00CU十四周年纪念徽章
日期:2015-12-22 16:50:40IT运维版块每日发帖之星
日期:2016-01-25 06:20:0015-16赛季CBA联赛之深圳
日期:2016-01-27 10:31:172016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之福建
日期:2016-04-07 11:25:2215-16赛季CBA联赛之青岛
日期:2016-04-29 18:02:5915-16赛季CBA联赛之北控
日期:2016-06-20 17:38:50技术图书徽章
日期:2016-07-19 13:54:03程序设计版块每日发帖之星
日期:2016-08-21 06:20:00
5 [报告]
发表于 2015-04-28 09:29 |只看该作者
回复 4# hellioncu


    也有点像sublime

论坛徽章:
0
6 [报告]
发表于 2015-04-28 10:38 |只看该作者
你这开头 一看就有错误。
Complex & Complex:perator%=(const Complex & c2)
{
        int(real_) %= int(c2.real_);
        int(imag_) %= int(c2.imag_);
        return *this;
}
强制类型转换后是不能作为左值的。实际上强制类型转换你可以理解为:一个常量(你需要的类型),这个常量的值为原变量的值。当然编译器具体是怎么做的,你去学习相关的知识。
按照你的本意,代码应该这样改:
Complex & Complex:perator%=(const Complex & c2)
{
        real_=int(real_)% int(c2.real_);
         imag_= int(imag_)%int(c2.imag_);
        return *this;
}

论坛徽章:
0
7 [报告]
发表于 2015-04-28 17:56 |只看该作者
谢谢大哥回复 6# shuxiaohua1989


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP