未定义标识符

边卓琳 2019-11-05 05:52:21
#ifndef _COMPLEX_
#define _COMPLEX_


class complex
{
public:
complex(double r = 0, double i = 0)
: re(r), im(i)//initialization line初始值列
{}
complex& operator += (const complex&);

double real() const { return re; }//const 不变的量
double imag() const { return im; }
private://私有的,不想让其他函数调用
double re, im;

friend complex& _doapl(complex*, const complex&);//友元可以调用内部数据
};
//成员函数
inline complex&
_doapl(complex* ths, const complex& r)//ths是一个指针
{
ths->re += r.re;
ths->im += r.im;
return *ths;//返回的是指针所代表的值
}
inline complex&//接口部分
complex::operator +=(const complex& r)
{
return _doapl(this, r);
}
//非成员(全局)函数
//操作符重载
inline complex//下面一定是一个临时(本地)变量不能返回引用
operator + (const complex& x, const complex& y)
{
return complex(real(x) + real(y), imag(x) + imag(y));
}
inline complex
operator +(const complex& x, double y)
{
return complex(real(x) + y, imag(x));
}
inline complex
operator +(double x, const complex& y)
{
return complex(x + real(y), imag(y));
}
#include <iostream>
using namespace std;
ostream&
operator << (ostream& os, const complex& x)
{
return os << '(' << real(x) << ',' << imag(x) << ')';

#endif // !_COMPLEX_
}


显示real,imag未定义标识符
可我是从标准库中摘下来的,不知道哪出问题了
...全文
499 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
铖邑 2019-11-05
  • 打赏
  • 举报
回复
能解决最好了,至于用什么方法自己去取舍就好了
边卓琳 2019-11-05
  • 打赏
  • 举报
回复
引用 3 楼 SuperDay 的回复:
嗯,这样也是可以的
按照你一开始的办法也可以 但是那样的话,后面的使用就不方便了。我想先跟着已有的写一下,尽量少作改动。 总之很感谢了
铖邑 2019-11-05
  • 打赏
  • 举报
回复
嗯,这样也是可以的
边卓琳 2019-11-05
  • 打赏
  • 举报
回复
引用 1 楼 SuperDay 的回复:
写错了吧?应该是x.real() y.real() x.imag() y.imag()
我少写了两个函数 inline double real(const complex& x) { return x.real(); } inline double imag(const complex& x) { return x.imag(); } 这样就好了
铖邑 2019-11-05
  • 打赏
  • 举报
回复
写错了吧?应该是x.real() y.real() x.imag() y.imag()

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧