请大家帮忙,请大家帮忙改错

请大家帮忙改错 - 故障解答 - 电脑教程网

请大家帮忙改错

日期:2007-02-09   荐:
请大家帮忙改错#include "stdafx.h"#include <iostream.h> class Count { public: Count(int c){m_Count=c;}; friend Count operator (Count aa,Count bb); friend Count operator (int cc,const Count& dd); void Output(); private: int m_Count; }; Count operator (Count aa,Count bb) //error C2143: syntax error : missing ';'before ' '//fatal error C1004: unexpected end of file found{ return ((aa.m_Count bb.m_Count)); } int Count operator (int cc,const Count& dd) { if(cc dd.m_Count<0) return 0; else if(cc dd.m_Count<2000) return int(cc dd.m_Count); else if(cc dd.m_Count>=2000&&cc dd.m_Count<3000) return int((cc dd.m_Count)*1.1); else return int((cc dd.m_Count)*1.2);} void Count::Output() { cout<<"结算电量为:"<<m_Count<<endl; } void main() { Count a(1000),b(4000),c(5000); Count d(0); d = a b c; d.Output(); } =重载了吗?我可能错了Count operator (Count aa,Count bb) //error C2143: syntax error : missing ';'before ' '改成Count Count::operator (Count aa,Count bb) 试试请说详细一点好象在类外边重载的时候应该加上类作用域运算符。呵呵,我也菜,不是很确定哈。tsrs(调试人生) 运算符重载为友元函数,可不加呵呵~~~~~~~~~~~~~~~~~~~~~~~学习……把重载函数定义放到类里边可以解决。你都是些语法类的错误,你还要多看看书熟悉下,帮你改了,自己看吧#include <stdafx.h>#include <iostream.h> class Count { friend int operator (Count aa,Count bb); //友元一般写在最前面,且你的返回类型不对 friend int operator (int cc,const Count& dd);public: Count(int c){m_Count=c;} //此处不是声明,是定义}后不加分号 void Output(); private: int m_Count; }; int operator (Count aa,Count bb) { return ((aa.m_Count bb.m_Count)); } int operator (int cc,const Count& dd) { if(cc dd.m_Count<0) return 0; else if(cc dd.m_Count<2000) return int(cc dd.m_Count); else if(cc dd.m_Count>=2000 && cc dd.m_Count<3000) return int((cc dd.m_Count)*1.1); else return int((cc dd.m_Count)*1.2);} void Count::Output() { cout<<"结算电量为:"<<m_Count<<endl; } void main() { Count a(1000),b(4000),c(5000); Count d(0); d = a b c; d.Output(); }#include <iostream.h> class Count { public: Count(int c){m_Count=c;};friend Count operator (Count aa,Count bb){Count temp(0);temp.m_Count = aa.m_Count bb.m_Count;return ( temp); }friend Count operator (int cc,const Count& dd){if(cc dd.m_Count<0)return 0; else if(cc dd.m_Count<2000)return int(cc dd.m_Count);else if(cc dd.m_Count>=2000&&cc dd.m_Count<3000)return int((cc dd.m_Count)*1.1);elsereturn int((cc dd.m_Count)*1.2);}void Output();private: int m_Count; };void Count::Output() { cout<<"结算电量为:"<<m_Count<<endl; } void main() { Count a(1000),b(4000),c(5000); Count d(0); d = a b c; d.Output(); }通过了。偶太菜了,不知道咋回事,正查资料找原因,呵呵……返回值的临时类应该有的就是return Count(对象1.数据成员 对象2.数据成员);Count(int c){m_Count=c;}; 这后面不应该有分号,C 有时候就是会因为很小的细节而报错的
标签: