| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2234 人关注过本帖
标题:照着书上的例题打的,编译出一大堆错误
只看楼主 加入收藏
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:9 
照着书上的例题打的,编译出一大堆错误
用的vc6.0编译器,函数及运算符的重载
#include <iostream>
using namespace std;
class String
{public:
String(){p=NULL;}
String()(char *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;
};
String::String(char *str)
{p=str;}

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(string2.p,string2.p)==0)
return true;
else
return false;
}
void compare(String &string1,String &string2)
{if(operator>(string1,string2)==1)
{string.display();cout<<">";string2.display();}
else
 if(operator<(string1,string2)==1)
 {string1.display();cout<<"<";string2.display();}
else
 if(operator==(string1,string2)==1)
 {string1.display();cout<<"=";string2.display();}
 cout<<endl;
}
int main()
{String string1("Hello"),string2("Book"),string3("Computer"),string4("Hello");
 compare(string1,string2);
 compare(string2,string3);
 compare(string1,string4);
 return 0;
}
2016-01-27 21:15
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
什么叫function returns function?
2016-01-27 21:16
仰望星空的
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:50
专家分:248
注 册:2015-9-28
收藏
得分:0 
operator是重载的函数还是变量啊?
2016-01-28 08:46
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:5 
如果你是照着书自学的,请把那本书扫掉;
如果是老师教的,请务必去总务处举报你的老师误人子弟
这是很严肃的事,从代码的每一处都可以看出你的老师是一点儿都不会C++,甚至是完全不理解C++为什么要这样。

为了给你示范,我随手写了一段,可能有错误,但不要紧,因为我示范的是C++,不是具体算法
程序代码:
#include <iostream>
#include <cstring>

class String
{
public:
    String();
    ~String();
    String( const char* str );
    String( const String& s );
    String& operator=( const char* str );
    String& operator=( const String& s );

    friend bool operator>( const char* lhs, const String& rhs );
    friend bool operator>=( const char* lhs, const String& rhs );
    friend bool operator<( const char* lhs, const String& rhs );
    friend bool operator<=( const char* lhs, const String& rhs );
    friend bool operator==( const char* lhs, const String& rhs );
    friend bool operator!=( const char* lhs, const String& rhs );

    friend bool operator>( const String& lhs, const char* rhs );
    friend bool operator>=( const String& lhs, const char* rhs );
    friend bool operator<( const String& lhs, const char* rhs );
    friend bool operator<=( const String& lhs, const char* rhs );
    friend bool operator==( const String& lhs, const char* rhs );
    friend bool operator!=( const String& lhs, const char* rhs );

    friend bool operator>( const String& lhs, const String& rhs );
    friend bool operator>=( const String& lhs, const String& rhs );
    friend bool operator<( const String& lhs, const String& rhs );
    friend bool operator<=( const String& lhs, const String& rhs );
    friend bool operator==( const String& lhs, const String& rhs );
    friend bool operator!=( const String& lhs, const String& rhs );

    friend std::ostream& operator<<( std::ostream& os, const String& s );
    friend std::istream& operator>>( std::istream& in, String& s );

private:
    char* p_;
};

String::String() try: p_(new char[1])
{
    p_[0] = '\0';
} catch( ... ) { throw; }
String::~String()
{
    delete[] p_;
}
String::String( const char* str ) try: p_(new char[strlen(str)+1])
{
    strcpy( p_, str );
} catch( ... ) { throw; }
String::String( const String& s ) try: p_(new char[strlen(s.p_)+1])
{
    strcpy( p_, s.p_ );
} catch( ... ) { throw; }
String& String::operator=( const char* str )
{
    delete[] p_;
    p_ = new char[ strlen(str)+1 ];
    strcpy( p_, str );
    return *this;
}
String& String::operator=( const String& s )
{
    if( this != &s )
    {
        delete[] p_;
        p_ = new char[ strlen(s.p_)+1 ];
        strcpy( p_, s.p_ );
    }
    return *this;
}
bool operator>( const String& lhs, const String& rhs )
{
    return strcmp(lhs.p_,rhs.p_) > 0;
}
bool operator<( const String& lhs, const String& rhs )
{
    return strcmp(lhs.p_,rhs.p_) < 0;
}
bool operator==( const String& lhs, const String& rhs )
{
    return strcmp(lhs.p_,rhs.p_) == 0;
}
std::ostream& operator<<( std::ostream& os, const String& s )
{
    return os<<s.p_;
}
/* 必要,但暂未用到的部分,不写了*/

using namespace std;

void compare( const String& s1, const String& s2 )
{
    if( s1 > s2 )
        cout << s1 << " > " << s2 << '\n';
    else if( s1 < s2 )
        cout << s1 << " < " << s2 << '\n';
    else
        cout << s1 << " = " << s2 << '\n';
}

int main( void )
{
    String string1("Hello");
    String string2("Book");
    String string3("Computer");
    String string4("Hello");

    compare( string1, string2 );
    compare( string2, string3 );
    compare( string1, string4 );

    return 0;
}

2016-01-28 09:10
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
如果照书上敲的,楼主做做好事,把书名说给大家知道。至少大家可以引以为荐。感觉跟百度学还要靠谱一些。
2016-01-28 10:18
fedfsfuups
Rank: 2
等 级:论坛游民
帖 子:1
专家分:15
注 册:2016-1-28
收藏
得分:15 
我拷贝了代码,发现几处错误的地方
String()(char *str); // 这个是什么格式啊?String(char *str); 这样的吧

string.display(); cout << ">"; string2.display();    // 这个string.display(); 是什么东西啊?看意思是string1.display();吧
2016-01-28 11:11
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
谭浩强的c++程序设计(第3版)p309
2016-01-28 11:12
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
以下是引用fedfsfuups在2016-1-28 11:11:22的发言:

我拷贝了代码,发现几处错误的地方
String()(char *str); // 这个是什么格式啊?String(char *str); 这样的吧
 
string.display(); cout << ">"; string2.display();    // 这个string.display(); 是什么东西啊?看意思是string1.display();吧
你太有才了!这两处错误我竟然没发现!正解啊!
2016-01-28 11:18
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
我打错代码了。和书本身无关!
2016-01-28 11:20
归云居
Rank: 2
等 级:论坛游民
帖 子:15
专家分:36
注 册:2016-1-27
收藏
得分:0 
没有用过,我都是先和视频学再看书,还有楼主的代码太乱了
2016-01-28 11:21
快速回复:照着书上的例题打的,编译出一大堆错误
数据加载中...
 
   



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

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