二进制“<<”: 没有找到接受“const complex” 类型的右操作数的运算符(或没有可接受的转换)
重载符号《为友元函数,代码如下,编译时提示“二进制“<<”: 没有找到接受“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;
}
#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;
}
...全文
相关推荐
详解C++中const_cast与reinterpret_cast运算符的用法
使用reinterpret_cast时,我们需要格外小心,因为它不进行任何数据转换或处理,仅仅是告诉编译器以不同的方式解释原有的二进制数据。
C++类型转换运算符的实例详解
"本文详细介绍了C++中的四种类型转换运算符:dynamic_cast、const_cast、static_cast和reinterpret_cast,以及它们在实际编程中的应用。"C++中的类
两个二维数组相加,用成员函数重载运算符“+”和“-”
### 两个二维数组相加与相减:使用成员函数重载运算符“+”和“-”在C++中,运算符重载是一种强大的特性,它允许程序员改变内置运算符的行为,使其适用于自定义类型(如类或结构)。
C++中用于强制类型转换的四个运算符
本文详细介绍了C++中的三种类型转换运算符:const_cast、reinterpret_cast和static_cast。const_cast用于移除变量的const或volatile属性,允许修改
详解C++中的增量运算符++和减量运算符--的用法
**枚举类型与增量运算**枚举类型(`enum`)通常不支持后缀增量和减量操作,因为这些操作是针对数值类型的,而枚举类型虽然可以隐式转换为整型,但直接使用增量运算符可能不被编译器接受。


