鱼C论坛

 找回密码
 立即注册
查看: 2195|回复: 4

[已解决]c++ “=” 重载

[复制链接]
发表于 2019-6-8 12:02:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 kkk222 于 2019-6-8 12:07 编辑

  • #include <iostream>
  • #include <cstring>
  • using namespace std;
  • class Complex
  • {
  • private:
  •         double r, i;
  • public:
  •         void Print()
  •         {
  •                 cout << r << "+" << i << "i" << endl;
  •         }
  •         Complex operator =(const string& s);
  • };
  • Complex Complex::operator =(const string& s)
  • {
  •         Complex temp;
  •         int pos = s.find("+",0);
  •         string sTmp = s.substr(0, pos);
  •         temp.r = atof(sTmp.c_str());
  •         sTmp = s.substr(pos + 1, s.length() - pos - 2);
  •         temp.i = atof(sTmp.c_str());
  •         cout << temp.r << "+" << temp.i << "i" << endl;
  •         return temp;
  • }
  • int main()
  • {
  •         Complex a;
  •         a = "3+4i"; a.Print();
  •         a = "5+6i"; a.Print();
  •         return 0;
  • }


程序的目的是把字符串中“+”前面的数赋值给对象的实部,“+”后面到“i”前面的数赋值给虚部

程序的结果:
1.JPG

对象a的实部和虚部怎么都是指数?
最佳答案
2019-6-8 20:12:34
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <string>

  5. using namespace std;
  6. class Complex
  7. {
  8. private:
  9.         double r, i;
  10. public:
  11.         void Print()
  12.         {
  13.                 cout << r << "+" << i << "i" << endl;
  14.         }
  15.         Complex & operator =(const string& s);
  16. };
  17. Complex & Complex::operator =(const string& s)
  18. {
  19.         Complex temp;
  20.         int pos = s.find("+",0);
  21.         string sTmp = s.substr(0, pos);
  22.         temp.r = atof(sTmp.c_str());
  23.         sTmp = s.substr(pos + 1, s.length() - pos - 2);
  24.         temp.i = atof(sTmp.c_str());
  25.         cout << temp.r << "+" << temp.i << "i" << endl;
  26.         this->r = temp.r;
  27.         this->i = temp.i;
  28.         return *this;
  29. }
  30. int main()
  31. {
  32.         Complex a;
  33.         a = "3+4i"; a.Print();
  34.         a = "5+6i"; a.Print();
  35.         return 0;
  36. }
复制代码


返回引用
鸣人不说暗话,我要最佳答案!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-6-8 19:55:50 | 显示全部楼层
为啥我编译不通过?
hello.cpp: In member function ‘Complex Complex::operator=(const string&)’:
hello.cpp:20:35: error: ‘atof’ was not declared in this scope
         temp.r = atof(sTmp.c_str());
                                   ^
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-8 20:12:34 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <string>

  5. using namespace std;
  6. class Complex
  7. {
  8. private:
  9.         double r, i;
  10. public:
  11.         void Print()
  12.         {
  13.                 cout << r << "+" << i << "i" << endl;
  14.         }
  15.         Complex & operator =(const string& s);
  16. };
  17. Complex & Complex::operator =(const string& s)
  18. {
  19.         Complex temp;
  20.         int pos = s.find("+",0);
  21.         string sTmp = s.substr(0, pos);
  22.         temp.r = atof(sTmp.c_str());
  23.         sTmp = s.substr(pos + 1, s.length() - pos - 2);
  24.         temp.i = atof(sTmp.c_str());
  25.         cout << temp.r << "+" << temp.i << "i" << endl;
  26.         this->r = temp.r;
  27.         this->i = temp.i;
  28.         return *this;
  29. }
  30. int main()
  31. {
  32.         Complex a;
  33.         a = "3+4i"; a.Print();
  34.         a = "5+6i"; a.Print();
  35.         return 0;
  36. }
复制代码


返回引用
鸣人不说暗话,我要最佳答案!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-9 14:38:02 | 显示全部楼层
我就是个弟弟 发表于 2019-6-8 20:12
返回引用
鸣人不说暗话,我要最佳答案!!!

16行代码那里Complex & operator 的‘&’是引用吗?
为什么要返回 *this 而不能返回temp?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-9 20:06:45 | 显示全部楼层
kkk222 发表于 2019-6-9 14:38
16行代码那里Complex & operator 的‘&’是引用吗?
为什么要返回 *this 而不能返回temp?

htt*ps://w*ww.cnb*logs.com/zpc*dbky/p/502*7481.html
删掉星星
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-7 21:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表