| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1115 人关注过本帖
标题:求助:运算符重载问题
取消只看楼主 加入收藏
yjcf
Rank: 1
来 自:西安工业大学
等 级:新手上路
帖 子:145
专家分:0
注 册:2008-3-29
收藏
 问题点数:0 回复次数:1 
求助:运算符重载问题
// 运算符重载的集合应用
// p的定义有错误,暂时无法找到原因

#include "stdafx.h"
#include <iostream>
using namespace std;
class String
{
public:
    String(char *str):p(str){}
    friend bool operator>(String &string1,String &string2);
    friend bool operator<(String &string1,String &string2);
    friend bool operator==(String &string1,String &string2);
    void display();
private:
    char *p;
};

void String::display()
{
    cout<<p;
}

bool operator>(String &string1,String &string2)
{
    if(strcmp(string1.p,string2.p)>0)
        return true;
    else
        return false;
}

bool operator<(String &string1,String &string2)
{
    if(strcmp(string1.p,string2.p)<0)
        return true;
    else
        return false;
}

bool operator==(String &string1,String &string2)
{
    if(strcmp(string1.p,string2.p)==0)
        return true;
    else
        return false;
}

void compare(String &string1,String &string2)
{
    if(operator>(string1,string2)==1)
    {
        string1.display();
        cout<<">";
        string2.display();
    }

    if(operator<(string1,string2)==1)
    {
        string1.display();
        cout<<"<";
        string2.display();
    }

    if(operator==(string1,string2)==1)
    {
        string1.display();
        cout<<"=";
        string2.display'();
    }
}

int main()
{
    String string1("Hello"),string2("Book");
    String string3("Computer"),string4("Hello");
    compare(string1,string2);
    compare(string2,string3);
    compare(string1,string4);
    return 0;
}

编译时有很多错误,怀疑p的定义有错误,请问错在了哪里?
搜索更多相关主题的帖子: 运算符 重载 
2008-10-10 13:28
yjcf
Rank: 1
来 自:西安工业大学
等 级:新手上路
帖 子:145
专家分:0
注 册:2008-3-29
收藏
得分:0 
谢谢楼上各位,可我还是想不通错在了哪?

泾溪石险人竞慎,终岁不闻倾覆人。却是平流无石处,时时闻说有沉论。
2008-10-10 15:07
快速回复:求助:运算符重载问题
数据加载中...
 
   



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

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