注册 登录
编程论坛 C++教室

照着书上的例题打的,编译出一大堆错误

ehszt 发布于 2016-01-27 21:15, 2370 次点击
用的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;
}
9 回复
#2
ehszt2016-01-27 21:16
什么叫function returns function?
#3
仰望星空的2016-01-28 08:46
operator是重载的函数还是变量啊?
#4
rjsp2016-01-28 09:10
如果你是照着书自学的,请把那本书扫掉;
如果是老师教的,请务必去总务处举报你的老师误人子弟
这是很严肃的事,从代码的每一处都可以看出你的老师是一点儿都不会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;
}

#5
yangfrancis2016-01-28 10:18
如果照书上敲的,楼主做做好事,把书名说给大家知道。至少大家可以引以为荐。感觉跟百度学还要靠谱一些。
#6
fedfsfuups2016-01-28 11:11
我拷贝了代码,发现几处错误的地方
String()(char *str); // 这个是什么格式啊?String(char *str); 这样的吧

string.display(); cout << ">"; string2.display();    // 这个string.display(); 是什么东西啊?看意思是string1.display();吧
#7
ehszt2016-01-28 11:12
谭浩强的c++程序设计(第3版)p309
#8
ehszt2016-01-28 11:18
以下是引用fedfsfuups在2016-1-28 11:11:22的发言:

我拷贝了代码,发现几处错误的地方
String()(char *str); // 这个是什么格式啊?String(char *str); 这样的吧
 
string.display(); cout << ">"; string2.display();    // 这个string.display(); 是什么东西啊?看意思是string1.display();吧
你太有才了!这两处错误我竟然没发现!正解啊!
#9
ehszt2016-01-28 11:20
我打错代码了。和书本身无关!
#10
归云居2016-01-28 11:21
没有用过,我都是先和视频学再看书,还有楼主的代码太乱了
1