关于模板的 问题

s__c__007 2009-04-12 05:01:36

#include<iostream.h>
template <class T>
class complex
{
private:
T real;
T imag;
public:
complex(T r=0,T i=0)
{
real=r;
imag=i;
}
void print();
friend complex operator+(const complex &a,const complex &b);
friend complex operator-(const complex &a,const complex &b);
complex operator*(const complex &a);
complex operator/(const complex &a);
friend complex operator++(complex &a);
complex operator++(int);
friend ostream& operator<<(ostream &s,const complex &c);
friend istream& operator>>(istream &s,complex &c);
};
template <class T>
void complex<T>::print()
{
cout<<real;
if(imag)
{
if(imag>0)cout<<"+";
cout<<imag<<"i";
cout<<endl;
}
}
template <class T>
complex<T> operator+(const complex<T> &a,const complex<T> &b)
{
complex<T> temp;
temp.real=a.real+b.real;
temp.imag=a.imag+b.imag;
return temp;
}
template <class T>
complex<T> operator-(const complex<T> &a,const complex<T> &b)
{
complex<T> temp;
temp.real=a.real-b.real;
temp.imag=a.imag-b.imag;
return temp;
}
template <class T>
complex<T> complex<T>::operator*(const complex<T> &a)
{
complex<T> temp;
temp.real=a.real*real-a.imag*imag;
temp.imag=a.real*imag+real*a.imag;
return temp;
}
template <class T>
complex<T> complex<T>::operator/(const complex<T> &a)
{
complex<T> temp;
temp.real=(real*a.real+a.imag*imag)/(a.real*a.real+a.imag*a.imag);
temp.imag=(real*(-a.imag)+imag*a.real)/(a.real*a.real+a.imag*a.imag);
return temp;
}
template <class T>
complex<T> operator++(complex<T> &a)
{
++a.real;
++a.imag;
return a;
}
template <class T>
complex<T> complex<T>::operator++(int)
{
complex<T> temp(*this);
real++;
imag++;
return temp;
}
/*template <class T>
ostream& operator<<(ostream &s,const complex<T> &c)
{
s<<c.real;
if(c.imag)
{
if(c.imag>0) s<<"+";
s<<c.imag<<"i";
s<<endl;
}
return s;
}*/
template <class T>
istream& operator>>(istream &s,complex<T> &c)
{
s>>c.real;
s>>c.imag;
return s;
}

int main()
{
complex <double> A1(2.3,4.6),A2(3.6,2.8);
complex <double> A3,A4,A5,A6;
A3=A1+A2;
A4=A1-A2;
A5=A1*A2;
A6=A1/A2;
cout<<"A1=";
cout<<A1;
cout<<endl<<"A2=";
cout<<A2;
cout<<endl<<"A3=A1+A2=";
cout<<A3;
cout<<endl<<"A4=A1-A2=";
cout<<A4;
cout<<endl<<"A5=A1*A2=";
cout<<A5;
cout<<endl<<"A6=A1/A2=";
cout<<A6;
A3=++A1;
cout<<endl<<"after A3=++A1"<<endl;
cout<<"A1=";
cout<<A1;
cout<<endl<<"A3=";
cout<<A3;
A4=A2++;
cout<<endl<<"after A4=A2++"<<endl;
cout<<"A2=";
cout<<A2;
cout<<endl<<"A4=";
cout<<A4;
return 0;
}


在注释的地方如果调用print函数该怎么写?
...全文
393 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaocha 2009-04-12
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 s__c__007 的回复:]
谢谢 这里为什么加const啊 ???
[/Quote]

加 const 表示 print 函数不会改变类的的变量,
相当于 print 内的 this 指针为 const 类型,

避免了错误:
E:\学习\C++\程序\实验\8-1.cpp(85) : error C2662: 'print' : cannot convert 'this' pointer from 'const class complex <double>' to 'class complex <double> &' Conversion loses qualifiers



template <class T> ostream& operator<<(ostream &s,const complex<T> &c)
{
c.print(s);
}
这种需要const complex<T>的函数可以调用
love514425 2009-04-12
  • 打赏
  • 举报
回复
> print不是运算符号,重载也是重载函数.
> 为何要在模板里讨论C风格的printf?
> 建议LZ好好看看 <<Effictive C++>>
s__c__007 2009-04-12
  • 打赏
  • 举报
回复
谢谢 这里为什么加const啊 ???
xiaocha 2009-04-12
  • 打赏
  • 举报
回复
void print(ostream &s); ==> void print(ostream &s) const;

c.print(s); ==> c.print(s); return s;
s__c__007 2009-04-12
  • 打赏
  • 举报
回复
#include<iostream.h>
template <class T>
class complex
{
private:
T real;
T imag;
public:
complex(T r=0,T i=0)
{
real=r;
imag=i;
}
void print(ostream &s);
friend complex operator+(const complex &a,const complex &b);
friend complex operator-(const complex &a,const complex &b);
complex operator*(const complex &a);
complex operator/(const complex &a);
friend complex operator++(complex &a);
complex operator++(int);
friend ostream& operator<<(ostream &s,const complex &c);
friend istream& operator>>(istream &s,complex &c);
};
template <class T>
void complex<T>::print(ostream &s)
{
s<<real;
if(imag)
{
if(imag>0)s<<"+";
s<<imag<<"i";
s<<endl;
}
}
template <class T>
complex<T> operator+(const complex<T> &a,const complex<T> &b)
{
complex<T> temp;
temp.real=a.real+b.real;
temp.imag=a.imag+b.imag;
return temp;
}
template <class T>
complex<T> operator-(const complex<T> &a,const complex<T> &b)
{
complex<T> temp;
temp.real=a.real-b.real;
temp.imag=a.imag-b.imag;
return temp;
}
template <class T>
complex<T> complex<T>::operator*(const complex<T> &a)
{
complex<T> temp;
temp.real=a.real*real-a.imag*imag;
temp.imag=a.real*imag+real*a.imag;
return temp;
}
template <class T>
complex<T> complex<T>::operator/(const complex<T> &a)
{
complex<T> temp;
temp.real=(real*a.real+a.imag*imag)/(a.real*a.real+a.imag*a.imag);
temp.imag=(real*(-a.imag)+imag*a.real)/(a.real*a.real+a.imag*a.imag);
return temp;
}
template <class T>
complex<T> operator++(complex<T> &a)
{
++a.real;
++a.imag;
return a;
}
template <class T>
complex<T> complex<T>::operator++(int)
{
complex<T> temp(*this);
real++;
imag++;
return temp;
}
template <class T>
ostream& operator<<(ostream &s,const complex<T> &c)
{
c.print(s);
}
template <class T>
istream& operator>>(istream &s,complex<T> &c)
{
s>>c.real;
s>>c.imag;
return s;
}

int main()
{
complex <double> A1(2.3,4.6),A2(3.6,2.8);
complex <double> A3,A4,A5,A6;
A3=A1+A2;
A4=A1-A2;
A5=A1*A2;
A6=A1/A2;
cout<<"A1=";
cout<<A1;
cout<<endl<<"A2=";
cout<<A2;
cout<<endl<<"A3=A1+A2=";
cout<<A3;
cout<<endl<<"A4=A1-A2=";
cout<<A4;
cout<<endl<<"A5=A1*A2=";
cout<<A5;
cout<<endl<<"A6=A1/A2=";
cout<<A6;
A3=++A1;
cout<<endl<<"after A3=++A1"<<endl;
cout<<"A1=";
cout<<A1;
cout<<endl<<"A3=";
cout<<A3;
A4=A2++;
cout<<endl<<"after A4=A2++"<<endl;
cout<<"A2=";
cout<<A2;
cout<<endl<<"A4=";
cout<<A4;
return 0;
}


E:\学习\C++\程序\实验\8-1.cpp(85) : error C2662: 'print' : cannot convert 'this' pointer from 'const class complex<double>' to 'class complex<double> &'
Conversion loses qualifiers
E:\学习\C++\程序\实验\8-1.cpp(104) : see reference to function template instantiation 'class ostream &__cdecl operator <<(class ostream &,const class complex<double> &)' being compiled
执行 cl.exe 时出错.
xiaocha 2009-04-12
  • 打赏
  • 举报
回复
11楼没细想,6楼应该没错呀,把你的代码和错误提示再贴一下
xiaocha 2009-04-12
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 s__c__007 的回复:]
依然有错
[/Quote]
错误提示?
s__c__007 2009-04-12
  • 打赏
  • 举报
回复
这样和题目意思不就反了吗???
题目要求:重载operator < <函数的 代码中可以 直接调用原来已经具有输出功能的print()成员函数
xiaocha 2009-04-12
  • 打赏
  • 举报
回复
是的,两者关系反了,这样就行了:

template <class T>
void complex <T>::print(ostream &s)
{
s << *this;
}
然后
template <class T>
ostream& operator <<(ostream &s,const complex <T> &c)
{
s << real;
if(imag)
{
if(imag>0) s << "+";
s << imag << "i";
//s << endl;
}
}
s__c__007 2009-04-12
  • 打赏
  • 举报
回复
依然有错
xiaocha 2009-04-12
  • 打赏
  • 举报
回复
void print(); 也要改改:void print(ostream &s);
s__c__007 2009-04-12
  • 打赏
  • 举报
回复
8-1.cpp
E:\学习\C++\程序\实验\8-1.cpp(34) : error C2244: 'complex<T>::print' : unable to resolve function overload
E:\学习\C++\程序\实验\8-1.cpp(35) : error C2954: template definitions cannot nest
执行 cl.exe 时出错.

有这两个错误
s__c__007 2009-04-12
  • 打赏
  • 举报
回复
题目要求:重载operator<<函数的 代码中可以 直接调用原来已经具有输出功能的print()成员函数
xiaocha 2009-04-12
  • 打赏
  • 举报
回复
改一下 print() 函数
template <class T>
void complex<T>::print(ostream &s)
{
s << real;
if(imag)
{
if(imag>0) s << "+";
s << imag << "i";
s << endl;
}
}
然后
template <class T>
ostream& operator<<(ostream &s,const complex<T> &c)
{
c.print(s);
}
gao125210 2009-04-12
  • 打赏
  • 举报
回复

/*template <class T>
ostream& operator<<(ostream &s,const complex<T> &c)
{
s<<c.real;
if(c.imag)
{
if(c.imag>0) s<<"+";
s<<c.imag<<"i";
s<<endl;
}
return s;
}*/


貌似真不好弄啊,print不会识别%T吧,呵呵
  • 打赏
  • 举报
回复
print不是运算符号,重载也是重载函数.
为何要在模板里讨论C风格的printf?这么奇怪.
s__c__007 2009-04-12
  • 打赏
  • 举报
回复
在上面有对成员函数print的定义,这里向直接调用print函数啊。。。
ostream& operator<<(ostream &s,const complex<T> &c)
就是这个的函数体中直接用print函数
「已注销」 2009-04-12
  • 打赏
  • 举报
回复
直接print()
mengde007 2009-04-12
  • 打赏
  • 举报
回复

/*template <class T>
ostream& operator<<(ostream &s,const complex<T> &c)
{
s<<c.real;
if(c.imag)
{
if(c.imag>0) s<<"+";
s<<c.imag<<"i";
s<<endl;
}
return s;
}*/

不是已经重载了cout吗、楼主想重载print吗
内容概要:本文系统研究了构网型变流器的正负序阻抗解耦特性及其在弱电网环境下的稳定性表现,重点依托Matlab/Simulink仿真平台,构建了详细的阻抗数学模型,设计了解耦控制策略,并采用小信号扫频法进行频域辨识与稳定性验证。研究深入探讨了构网型变流器与传统跟网型逆变器在正负序阻抗特性上的本质差异,结合虚拟同步发电机(VSG)等先进控制技术,分析其在抑制宽频带振荡、削弱锁相环动态耦合等方面的优越性。文中不仅提供了完整的仿真模型与MATLAB代码实现,还整合了光伏、风电、储能、微电网等多类新能源系统的阻抗建模与稳定性分析资源,形成了一套面向新型电力系统稳定性的综合性技术资料体系,具有较强的科研复现与工程参考价值。; 适合人群:面向具备电力电子、电力系统自动化、新能源并网等专业背景的研究生、高校教师及工程技术人员,特别适用于从事阻抗建模、小干扰稳定性分析、宽频振荡机理研究以及撰写高水平学术论文的科研工作者。; 使用场景及目标:①掌握构网型变流器正负序阻抗建模与扫频辨识的仿真方法;②深入理解VSG等构网型控制在弱电网中提升稳定性的内在机理;③复现顶刊论文中的阻抗分析流程与稳定性判据应用;④利用提供的成熟模型与代码加速科研进程,支撑课题研究与学术成果产出。; 阅读建议:建议结合文中提供的Simulink模型与MATLAB代码,按照“理论建模—仿真搭建—扫频激励—频响提取—Nyquist判据分析”的完整流程进行实践操作,重点关注扫频信号的注入方式、频率范围设置及阻抗曲线的物理意义解读,并参考博士论文复现案例深化对复杂动态耦合问题的理解。

65,211

社区成员

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

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