| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 336 人关注过本帖
标题:全局运算符重载问题
取消只看楼主 加入收藏
哈哥
Rank: 1
等 级:新手上路
帖 子:17
专家分:2
注 册:2012-6-28
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:0 
全局运算符重载问题
程序代码:
// 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.
2012-07-11 21:17
快速回复:全局运算符重载问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016154 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved