鱼C论坛

 找回密码
 立即注册
查看: 1743|回复: 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
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>

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;
        this->r = temp.r;
        this->i = temp.i;
        return *this;
}
int main()
{
        Complex a;
        a = "3+4i"; a.Print();
        a = "5+6i"; a.Print();
        return 0;
}

返回引用
鸣人不说暗话,我要最佳答案!!!
想知道小甲鱼最近在做啥?请访问 -> 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());
                                   ^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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;
        this->r = temp.r;
        this->i = temp.i;
        return *this;
}
int main()
{
        Complex a;
        a = "3+4i"; a.Print();
        a = "5+6i"; a.Print();
        return 0;
}

返回引用
鸣人不说暗话,我要最佳答案!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

16行代码那里Complex & operator 的‘&’是引用吗?
为什么要返回 *this 而不能返回temp?
想知道小甲鱼最近在做啥?请访问 -> 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
删掉星星
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 21:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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