| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 815 人关注过本帖
标题:又是编译不能通过。。。唉
只看楼主 加入收藏
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
结帖率:91.43%
收藏
已结贴  问题点数:20 回复次数:2 
又是编译不能通过。。。唉
程序代码:
#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;
}
2011-10-11 22:12
维海
Rank: 2
等 级:论坛游民
帖 子:23
专家分:53
注 册:2010-11-25
收藏
得分:20 
回复 楼主 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;

 }
现在可以了,看看吧
2011-10-11 23:38
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
收藏
得分:0 
成员函数运算符重载错误
2011-10-12 10:43
快速回复:又是编译不能通过。。。唉
数据加载中...
 
   



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

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