二进制“<<”: 没有找到接受“const complex” 类型的右操作数的运算符(或没有可接受的转换)

liuchangqingsb 2014-06-16 02:43:16
重载符号《为友元函数,代码如下,编译时提示“二进制“<<”: 没有找到接受“const complex<T>” 类型的右操作数的运算符(或没有可接受的转换)”,请高手帮助分析原因,觉得声明和实现没什么问题!!
#include "stdafx.h"
#include<iostream>
using namespace std;

template<class T>
class complex
{
private:
double _r;
double _i;

public:
complex(double,double);
const complex<T> operator+(const complex<T>& a);
const complex<T> operator-(const complex<T>& a);
const complex<T> operator*(const complex<T>& a);
const complex<T> operator/(const complex<T>& a);
void get_r() const
{
cout<<_r<<endl;
}
// T& operator[](int index);
// const T& operator[](int index) const;
// template<class T>
friend iostream& operator<<<T>(iostream& out,const complex<T>& a);
// {
// out<<"("<<a._i<<","<<a._r<<")"<<endl;
// return out;
// }

};

template<class T>
complex<T>::complex(double a,double b)
{
_r=a;
_i=b;
}

template<class T>
const complex<T> complex<T>::operator +(const complex<T> &a)
{
return complex<T>(this->_i+a._i,this->_r+a._r);

}

template<class T>
const complex<T> complex<T>::operator -(const complex<T> &a)
{
return complex<T>(this->_i-a._i,this->_r-a._r);
}

template<class T>
const complex<T> complex<T>::operator *(const complex<T> &a)
{
return complex<T>(this->_i*a._i,this->_r*a._r);
}

template<class T>
const complex<T> complex<T>::operator /(const complex<T> &a)
{
if(a._i!=0 &&a._r!=0 )
return complex<T>(this->_i/a._i,this->_r/a._r);
exit(-1);
}
template<class T>
iostream& operator<<(iostream& out,const complex<T>& a)
{
out<<"("<<a._i<<","<<a._r<<")"<<endl;
return out;
}
int _tmain(int argc, _TCHAR* argv[])
{
complex<double> a1(2.3,4.6);
complex<double> a2(6.7,9.7);
cout<<(a1+a2)<<endl;
return 0;
}
...全文
720 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
buyong 2014-06-17
  • 打赏
  • 举报
回复
1. ostream 不是 iostream 2. 模板不支持定义和实现分离 一个例子:
    friend ostream& operator<<(ostream& out,const complex<T>& a)
    {
    out<<"("<<a._i<<","<<a._r<<")"<<endl;
    return out;
    }
liuchangqingsb 2014-06-17
  • 打赏
  • 举报
回复
能不能给我解释清楚一下啊各位大神
liuchangqingsb 2014-06-16
  • 打赏
  • 举报
回复
引用 2 楼 taodm 的回复:
答案很简单啊,cout的类型是啥?你真的去确认过?
iostream类型啊,我是菜鸟,麻烦您详细告我一下吧
taodm 2014-06-16
  • 打赏
  • 举报
回复
答案很简单啊,cout的类型是啥?你真的去确认过?
liuchangqingsb 2014-06-16
  • 打赏
  • 举报
回复
哪位高手帮我解答一下啊万分感谢

65,187

社区成员

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

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