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

又是编译不能通过。。。唉

thlgood 发布于 2011-10-11 22:12, 815 次点击
程序代码:
#include <iostream>
#include <cstring>
using namespace std;

class book{
    private:
        char name[20];
    public:
        const book operator +(const book &a, const book &b) const;
        void showbookname();
        void setname( char *str);
        char *getname();
        book(char *str);
};
const book book::operator +(const book &a, const book &b) const{
    char str_temp[20];
    strcpy(str_temp, (a.getname));
    strcat(str_temp, (b.getname));
    return book(str_temp);
}
char* book::getname(){
    return name;
}

void book::setname(char *str){
    strcat(name, str);
}
void book::showbookname(){
    cout << name << endl;
}
book::book(char *str){
    strcpy(name, str);
}
int main(){
    book book_one("Hello");
    book book_two("yes");
    book book_three = book_one + book_two;
    book book_four = book_one + "Okay!";
    book_three.showbookname();
    book_four.showbookname();
    cout << "Hello world!" << endl;
    return 0;
}
2 回复
#2
维海2011-10-11 23:38
回复 楼主 thlgood
程序代码:
#include <iostream>
#include <cstring>

 using namespace std;
class book{
     private:
         char name[20];
     public:
         const book operator +( const book &b) const;
         void showbookname();
         void setname( char *str);
         const char *getname ()const;
         book(char *str);//explicit book(char *str);
};

 const book book::operator +( const book &b) const{
     char str_temp[20];
     strcpy(str_temp, this->getname());
     strcat(str_temp, b.getname());
     return book(str_temp);

 }

 const char* book::getname ()const{
     return name;

 }
void book::setname(char *str){
     strcat(name, str);

 }

 void book::showbookname(){
     cout << name << endl;

 }

 book::book(char *str){
     strcpy(name, str);

 }

 int main(){
     book book_one("Hello");
     book book_two("yes");
     book book_three = book_one + book_two;
     book book_four = book_one + "Okay!";
     book_three.showbookname();
     book_four.showbookname();
     //cout << "Hello world!" << endl;
     return 0;

 }
现在可以了,看看吧
#3
lucky5635912011-10-12 10:43
成员函数运算符重载错误
1