重载operator<<的问题,请进!

broadarms 2005-04-07 03:52:36
template<typename T>
class Stack{
friend ostream&
operator<<(ostream&, const Stack<T>&);
...;
};

template<typename T>
Stack<T>::ostream&
operator<<(ostream& out, const Stack<T>& s)
{
//output the stack s from top to bottom
//output stack size
out<<"The stack has"<<(s.top+1)
<<"element(s)"<<endl;
//output stack elements
out<<"The element(s) frome top to bottom are"<<endl;
for(int i=s.top;i>=0;i--)
out<<s.stack[i]<<' ';
out<<endl;
return out;
}


$g++ -o stack stack.cpp
In file included from stack.cpp:2:
/home/freeman/cp/stack/Stack.h:13: warning: friend declaration `std::ostream&
operator<<(std::ostream&, const Stack<T>&)' declares a non-template function
/home/freeman/cp/stack/Stack.h:13: warning: (if this is not what you intended,
make sure the function template has already been declared and add <> after
the function name here) -Wno-non-template-friend disables this warning
/home/freeman/cp/stack/Stack.h:15: warning: friend declaration `std::istream&
operator>>(std::istream&, Stack<T>&)' declares a non-template function
/home/freeman/cp/stack/Stack.h:32: error: syntax error before `&' token
/home/freeman/cp/stack/Stack.h:32: confused by earlier errors, bailing out
...全文
102 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
broadarms 2005-04-12
  • 打赏
  • 举报
回复
我看书上也是去掉的,我没加stack::试过,问题更多。
晕!
langzi8818 2005-04-12
  • 打赏
  • 举报
回复
Stack<T>::ostream&
operator<<(ostream& out, const Stack<T>& s)
Stack<T>::去掉,友员不是一个成员
ysbcg 2005-04-12
  • 打赏
  • 举报
回复
去掉Stack<T>::

template<typename T>
ostream&
operator<<(。。。。
ysbcg 2005-04-12
  • 打赏
  • 举报
回复
template<typename T>
class Stack{
friend ostream&
operator<<(ostream&, const Stack<T>&);
};

template<typename T>
ostream&
operator<<(ostream& out, const Stack<T>& s) {
//output the stack s from top to bottom
//output stack size
out<<"The stack has"<<(s.top+1)
<<"element(s)"<<endl;
//output stack elements
out<<"The element(s) frome top to bottom are"<<endl;
for(int i=s.top;i>=0;i--)
out<<s.stack[i]<<' ';
out<<endl;
return out;
}
broadarms 2005-04-12
  • 打赏
  • 举报
回复
ding~
#include #include #include #include #include #include #define EPS 1e-5 //定义精度常数 using namespace std; //使用标准空间命名std namespace NameCComplex //定义命名空间NameCComplex { class CComplex ////定义一个CComplex类 { private: double Real,Image; public: CComplex(double real=0,double image=0) //构造函数 { Real=real; Image=image; } friend istream & operator>>(istream &is,CComplex &com); //重载输入 friend ostream & operator<(CComplex &com); int operator(CComplex &com) //重载运算符">",比较模的大小 { if(mod()>com.mod()) return 1; else return 0; } int CComplex::operator<(CComplex &com) { if(mod()>(istream &is,CComplex &com) //重载输入,可以输入a+bi的形式 { cout<>s; //用字符串的形式接受复数 int len=strlen(s); //求出字符串的长度 int n=0,sign=1; //n为当前从字符串中提取出来的数字,初始化为0;sign是难道符号,初始化为正 com.Image=com.Real=0; for(int k=0;koperator<<(ostream &os,CComplex &com) //重载输入 { if(fabs(com.Image)operator+(CComplex &com) //加法重载 { CComplex sum; sum.Real=Real+com.Real; //实部相加 sum.Image=Image+com.Image; //虚部相加 return sum; } CComplex CComplex::operator*(CComplex &com) //乘法重载 { CComplex multi; multi.Real=Real*com.Real-Image*com.Image; //乘积实部 multi.Image=Real*com.Image+Image*com.Real; //乘积虚部 return multi; } CComplex CComplex::operator-(CComplex &com) //减法重载 { CComplex sub; sub.Real=Real-com.Real; sub.Image=Image-com.Image; return sub; } CComplex CComplex::operator+=(CComplex &com) //重载加法赋值 { Real=Real+com.Real; Image=Image+com.Image; return *this; } CComplex CComplex::operator-=(CComplex &com) //重载减法赋值 { Real=Real-com.Real; Image=Image-com.Image; return *this; } CComplex CComplex::operator*=(CComplex &com) //重载乘法赋值 { double nReal=Real*com.Real-Image*com.Image; double nImage=Real*com.Image+Image*com.Real; Real=nReal; Image=nImage; return *this; } int CComplex::operator==(CComplex &com) //重载等于 { if(Real==com.Real && Image==com.Image) return 1; else return 0; } void Test(void) //测试函数 { user.nTest++; cout<<"共10道题,做100以内的加减运算,满分100分:\n"; double real1,real2,image1,image2,real3,real4,image3,image4; CComplex answer,temp; int score=0; char op; for(int i=0;i<=9;i++) { /////为复数产生随机值 real1=rand()%200-100; image1=rand()%200-100; real2=rand()%200-100; image2=rand()%200-100; CComplex a(real1,image1),b(real2,image2); real3=rand()%20-10; image3=rand()%20-10; real4=rand()%20-10; image4=rand()%20-10; CComplex c(real3,image3),d(real4,image4); op=rand()%3; //产生随机加减乘法运算的三个值 switch(op) { case 0: answer=a+b; cout<temp; //输入用户计算值 if(answer==temp) //比较用户计算值 { score+=10; } else { cout<<"此题做错了\n"; cout<<"正确答案为:"<num1; cout<>num2; sum=num1+num2; cout<>num1; int i=4; while(!(num1==Zero)) { sum=sum+num1; cout<<"第"<num1; i++; } cout<<"加法结果是:"<num1; cout<>num2; sub=num1-num2; cout<>num1; int i=4; while(!(num1==Zero)) { sub=sub-num1; cout<<"第"<num1; i++; } cout<<"减法结果是:"<num1; cout<>num2; mul=num1*num2; cout<>num1; int i=4; while(!(num1==Zero)) { mul*=num1; cout<<"第"<num1; i++; } cout<<"乘法结果是:"<num1; ++num1; cout<<"自加的结果为"<num1; --num1; cout<<"自减的结果为"<num1; cout<>num2; if(num1==num2) coutstrChoise; if(strcmp(strChoise,"1")==0) //用户选1则调用Add()函数 Add(); else if(strcmp(strChoise,"2")==0) //用户选2则调用Sub()函数 Sub(); else if(strcmp(strChoise,"3")==0) //用户选3则调用Test()函数 Test(); else if(strcmp(strChoise,"4")==0) //用户选4则调用Add()函数 Mul(); else if(strcmp(strChoise,"5")==0) //用户选5调用Add1()函数 Add1(); else if(strcmp(strChoise,"6")==0) //用户选6则调用Sub1()函数 Sub1(); else if(strcmp(strChoise,"7")==0) //用户选7则调用Compare()函数 compare(); else if(strcmp(strChoise,"0")==0) //用户选0则结束调用函数 { cout<<"\n\n\t欢迎下次继续使用复数计算器!\n\n"; break; } else { cout<<"\n\t输入错误,请按任意键继续!\n"; cin.get(); cin.get(); } } while((strcmp(strChoise,"0"))); SaveFile(); //调用用户资料保存函数 return 0; }

64,648

社区成员

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

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