我用VC2005做个仿制的string类,结果出问题了,想了6小时了。。。实在不想浪费生产力了,拿出来求助之。。

[复制链接]
查看11 | 回复2 | 2011-3-13 10:41:16 | 显示全部楼层 |阅读模式
//自己做个string类,以下是我的类的.h和.cpp:
/* mystring.h */

#ifndef MYSTRING1_H
#define MYSTRING1_H

typedef class MyString1
{
public:
MyString1(const char * =\"\") ;
MyString1(const MyString1
~MyString1() ;
const MyString1
void printfString1() const ;
private:
int size ;
char *stri1 ;
}Str ;

#endif

/* mystring.cpp */

#includestdio.h
#include\"mystring1.h\"
#includecstring
using std::strcpy ;

Str::MyString1(const char *p)
:size( (p!=0) ? strlen(p) : 0)
{
if(size!=0)
{
stri1=new char[size 1] ;
strcpy(stri1,p) ;
}
else
{
stri1=new char[1] ;
stri1[0]=NULL ;
}
}
Str::MyString1(const Str
for(int i=0;isize;i)
{
stri1=u.stri1 ;
}
}

void Str::printfString1() const
{

for(int i=0;isize 1;i)
printf(\"%c\",stri1) ;
}

Str::~MyString1()
{
delete [] stri1 ;
stri1=NULL ;
}

const Str
char *p ;
p=new char [x] ;
for(int i=0;isize;i)
p=stri1 ;
for(int i=0;is.size;i)
p[size i]=s.stri1 ;
p[x-1]=\'\\0\' ;
delete [] stri1 ;
stri1=NULL ;
size=x 1 ;
stri1=p ;
return *this ;
}
回复

使用道具 举报

千问 | 2011-3-13 10:41:16 | 显示全部楼层
<pre id=\"best-answer-content\" class=\"reply-text mb10\">const Str //这里错了,应该是const int x=size s.size 1,应该是字符串长度再加一个结束符的长度,所以应该是加1,不是减一
char *p ;
p=new char [x] ;
for(int i=0;isize;i)
p=stri1 ;
for(int i=0;is.size;i)
p[size i]=s.stri1 ;
p[x-1]=\'\\0\' ;
delete [] stri1 ;
stri1=NULL ;
size=x 1 ;
stri1=p ;
return *this ;
}
回复

使用道具 举报

千问 | 2011-3-13 10:41:16 | 显示全部楼层
<pre class=\"replyask-text\" id=\"content-176901\">不是啊,我编译链接都过了,应该没事啊。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行