| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 293 人关注过本帖
标题:各位仁兄能帮我看看怎么让这个编译通过
只看楼主 加入收藏
深藏依旧
Rank: 2
等 级:论坛游民
帖 子:45
专家分:93
注 册:2012-12-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
各位仁兄能帮我看看怎么让这个编译通过
#include<iostream>
#include<cstring>

using namespace std;

class String
{
private:
    char* _str;
public:
    String(char*str=NULL):_str(str)//构造函数
    {
   
        if(_str!=NULL)
        _str = new char[strlen(str)+1];
        strcpy(_str,str);
        
    }

    String(const String& other) : _str(other._str)//拷贝构造函数
    {
        if (other._str != NULL)   
        {
            _str = new char[strlen(other._str)+1];
            strcpy(_str, other._str);
        }
    }

   
    ~String()//析构函数
    {
        delete[] _str;
    }
public:

    friend ostream& operator<<(ostream& out,String& str)//“<<”运算符重载
    {
   
        out<<str._str;
        return out;
    }

  
  
    String operator+=(String other)//"+="运算符重载
   {
       int len = strlen(_str)+strlen(other._str);
       if(len==0)
       {
           return *this;
       };
       char* buffer = new char[len+1];
       strcpy(buffer,_str);
       strcat(buffer,other._str);

       delete[] _str;
       _str = buffer;
       return *this;
   }

    bool operator!=(String other)//"!="运算符重载
    {
        
           if(strcmp(_str,other._str)==0)
               return true;
           else
               return false;
    }

 
};


void main()
{
    String a="good student";
    String b="good";

    b+="student";
    if(a!=b)
    {
        cout<<"not same"<<endl;
    };
    char str[100];
    strcpy(str,(const char*)a);//这条语句通不过,在类中加什么操作能让编译通过?
    cout<<str<<endl;

}
搜索更多相关主题的帖子: private include public 
2012-12-08 12:58
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:10 
随手改了一下
但因为你这段代码中杂七杂八的错误太多,可能没改全,请见谅

程序代码:
#include <iostream>
#include <cstring>

class String
{
public:
    String( const char* str=NULL ) : str_(NULL)
    {
        if( str )
        {
            str_ = new char[strlen(str)+1];
            strcpy(str_, str);
        }
    }

    String( const String& other ) : str_(NULL)
    {
        if( other.str_ )
        {
            str_ = new char[strlen(other.str_)+1];
            strcpy(str_, other.str_);
        }
    }

    String& operator=( const String& other )
    {
        if( this != &other )
        {
            delete[] str_;
            str_ = other.str_;
            if( other.str_ )
            {
                str_ = new char[strlen(other.str_)+1];
                strcpy(str_, other.str_);
            }
        }
        return *this;
    }

    ~String()
    {
        delete[] str_;
    }

    operator const char*() const
    {
        return str_ ? str_ : "";
    }

    String& operator+=( const String& other )
    {
        if( !other.str_ )
            return *this;

        if( !str_ )
        {
            str_ = new char[strlen(other.str_)+1];
            strcpy(str_, other.str_);
            return *this;
        }

        size_t len1 = strlen(str_);
        size_t len2 = strlen(other.str_);
        char* buffer = new char[len1+len2+1];
        strcpy(buffer,str_);
        strcpy(buffer+len1,other.str_);
        delete[] str_;
        str_ = buffer;
        return *this;
    }

    bool operator!=( const String& other) const
    {
        return strcmp((const char*)str_, (const char*)other.str_) != 0;
    }

private:
    char* str_;
};

std::ostream& operator<<( std::ostream& out, const String& str )
{
    return out << (const char*)str;
}

using namespace std;

int main()
{
    String a = "good student";
    String b = "good";
    b += "student";

    if( a != b )
    {
        cout<<"not same"<<endl;
    }

    char str[100];
    strcpy( str, (const char*)a );
    cout << str << endl;

    return 0;
}

2012-12-08 13:38
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:10 
程序代码:
void main()

 {
     String a="good student";
     String b="good";

     b+="student";
     if(a!=b)
     {
         cout<<"not same"<<endl;
     };
     char str[100];
     strcpy(str,*(char**)&a); // 把a的地址转成 char * 的指针,也就是指针的指针,在访问就行了
     cout<<str<<endl;
} 


[ 本帖最后由 yuccn 于 2012-12-8 13:49 编辑 ]

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2012-12-08 13:46
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:0 
String 的第一个成员是char *
所以把a的地址转换成 char **,也就是指针的指针
在访问就行了

不过你这样的设计一定要保证第一个成员是 char * ,不能有虚函数什么的


我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2012-12-08 13:48
深藏依旧
Rank: 2
等 级:论坛游民
帖 子:45
专家分:93
注 册:2012-12-8
收藏
得分:0 
我表示十分的感谢  顺便说一下其实我的这段代码就一个错误啊  再次感谢!

厚积薄发
2012-12-08 13:57
快速回复:各位仁兄能帮我看看怎么让这个编译通过
数据加载中...
 
   



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

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