全局运算符重载问题
程序代码:
// resign5.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream.h> //****************************** CUser operator + (int nData,CUser &refUser) //声明全局函数 + 重载 { CUser eUser; eUser.m_nLevel = refUser.m_nLevel + nData; reuturn eUser; } //****************************** class CUser { private: int m_nLevel; public: //--------------------------------------- 构造函数 CUser() { m_nLevel = 1; } //--------------------------------------- +重载 CUser operator + (CUser &redoUser) { CUser aUser; aUser.m_nLevel = m_nLevel + redoUser.m_nLevel; return aUser; } //--------------------------------------- 读取操作员等级 int GetLevel() const { return this->m_nLevel; } //--------------------------------------- CUser operator + (int nData) { CUser dUser; dUser.m_nLevel = m_nLevel + nData; return dUser; } //---------------------------------------声明友元函数 +重载 friend CUser operator + (int nData,CUser &refUser); }; int main(int argc, char* argv[]) { //-------------------------------------- CUser Usera,Userb; CUser Userc = Usera + Userb; cout<<Userc.GetLevel()<<endl; //-------------------------------------- CUser Userd; CUser Userf = Userd + 10; cout<<Userf.GetLevel()<<endl; //-------------------------------------- CUser Usere; CUser Userg = 10 + Usere; cout<<Userg.GetLevel()<<endl; return 0; }为什么我声明全局+重载 出现错误?
D:\Project\resign5\resign5.cpp(7) : error C2143: syntax error : missing ';' before '+'
D:\Project\resign5\resign5.cpp(7) : error C2501: 'CUser' : missing storage-class or type specifiers
D:\Project\resign5\resign5.cpp(7) : fatal error C1004: unexpected end of file found
Error executing cl.exe.