| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1115 人关注过本帖
标题:求助:运算符重载问题
只看楼主 加入收藏
yjcf
Rank: 1
来 自:西安工业大学
等 级:新手上路
帖 子:145
专家分:0
注 册:2008-3-29
收藏
 问题点数:0 回复次数:6 
求助:运算符重载问题
// 运算符重载的集合应用
// 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
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
你们怎么都喜欢在C区问C++问题?!

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-10-10 13:37
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
程序代码:
#include <iostream>
using namespace std;
class String
{
public:
    String(const char *str):p(str){}
    friend bool operator>(const String& string1,const String& string2);
    friend bool operator<(const String& string1,const String& string2);
    friend bool operator==(const String& string1,const String& string2);
    void display() const;
private:
    const char *p;
};

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

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

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

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

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

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

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

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




干嘛要实现标准库里面有了的功能呢?

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-10-10 13:47
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
程序代码:
#include <iostream>
using namespace std;
class String
{
public:
    String(const char *str): p(str) {}
    bool operator>(const String& str) const{return strcmp(p, str.p) > 0;}
    bool operator<(const String& str) const{return strcmp(p, str.p) < 0;}
    bool operator==(const String& str) const{return strcmp(p, str.p) == 0;}
    void display() const{cout << p;}
private:
    const char *p;
};

void compare(const String& s1, const String& s2)
{
    s1.display();
    if (s1 == s2)
        cout << "==";
    else
        cout << (s1 < s2 ? '<' : '>');
    s2.display();
    cout <<endl;
}

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


干嘛不能让生活简单一些呢……其实我真的很想完全去掉这个类,直接使用std::string阿……

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-10-10 14:03
Eastsun
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:802
专家分:0
注 册:2006-12-14
收藏
得分:0 
你用的是VC6.0吧?
用gcc(DEV C++)就没错误了

My BlogClick Me
2008-10-10 14:08
yjcf
Rank: 1
来 自:西安工业大学
等 级:新手上路
帖 子:145
专家分:0
注 册:2008-3-29
收藏
得分:0 
谢谢楼上各位,可我还是想不通错在了哪?

泾溪石险人竞慎,终岁不闻倾覆人。却是平流无石处,时时闻说有沉论。
2008-10-10 15:07
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
错啊,你重新写一个compare函数,就知道了。

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-10-11 15:49
快速回复:求助:运算符重载问题
数据加载中...
 
   



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

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