按照书上第11章,运算符重载那一章,我把最后的那个string类做了一下,可是总是出错,有人
有书上那个string类的代码吗?
跪求!!!
谢谢大哥大姐们!!
//file1:mystring.h
#include <cstring>
namespace smr{
class String{
struct Srep{
char* s;
int sz;
int n;
Srep(int nsz,const char* p)
{
n=1;
sz=nsz;
s=new char[sz+1];
std::strcpy(s,p);
}
~Srep(){delete[] s;}
Srep* get_own_copy()
{
if(n==1) return this;
n--;
return new Srep(sz,s);
}
void assign(int nsz,const char* p)
{
if(sz!=nsz){
delete[] s;
sz=nsz;
s=new char[sz+1];
}
std::strcpy(s,p);
}
private:
Srep(const Srep&);
Srep& operator=(const Srep&);
};
Srep* rep;
public:
String();
String(const char*);
String(const String&);
String& operator=(const char*);
String& operator=(const String&);
~String();
void check(int i)const;
char read(int i)const;
void write(int i,char c);
int size()const;
class Rang{};
class Cref;
Cref operator[](int i);
char operator[](int i)const;
};
class String::Cref{
friend class String;
String& s;
int i;
Cref(String& ss,int ii):s(ss),i(ii){}
public:
operator char(){return s.read(i);}
void operator=(char c){s.write(i,c);}
};
}
// file2:myString.cpp
#include "myString.h"
namespace smr{
String::String()
{
rep=new Srep(0,"");
}
String::String(const char* s)
{
rep=new Srep(strlen(s),s);
}
String::String(const String& x)
{
x.rep->n++;
rep=x.rep;
}
String& String::operator=(const String& x)
{
x.rep->n++;
if(--rep->n==0) delete rep;
rep=x.rep;
return *this;
}
String& String::operator=(const char* s)
{
if(rep->n==1)
rep->assign(strlen(s),s);
else{
rep->n--;
rep=new Srep(strlen(s),s);
}
return *this;
}
String::~String()
{
if(--rep->n==0) delete rep;
}
inline void String::check(int i)const
{if(i<0 || rep->sz<=i) throw Rang();}
inline char String::read(int i)const
{return rep->s[i];}
inline void String::write(int i,char c)
{rep=rep->get_own_copy();rep->s[i]=c;}
inline int String::size()const{return rep->sz;}
inline String::Cref String::operator[](int i) //////////////////////////////编译的时候这个不能生成
{check(i);return String::Cref(*this,i);}
inline char String::operator[](int i)const
{check(i);return rep->s[i];}
}
d:\c++\main\mystring.h(71) : warning C4512: “smr::String::Cref”: 未能生成赋值运算符
d:\c++\main\mystring.h(63) : 参见“smr::String::Cref”的声明
//myString.h
#include <cstring>
namespace smr{
class String{
struct Srep{
char* s;
int sz;
int n;
Srep(int nsz,const char* p)
{
n=1;
sz=nsz;
s=new char[sz+1];
std::strcpy(s,p);
}
~Srep(){delete[] s;}
Srep* get_own_copy()
{
if(n==1) return this;
n--;
return new Srep(sz,s);
}
void assign(int nsz,const char* p)
{
if(sz!=nsz){
delete[] s;
sz=nsz;
s=new char[sz+1];
}
std::strcpy(s,p);
}
private:
Srep(const Srep&);
Srep& operator=(const Srep&);
};
Srep* rep;
public:
String();
String(const char*);
String(const String&);
String& operator=(const char*);
String& operator=(const String&);
~String();
void check(int i)const;
char read(int i)const;
void write(int i,char c);
int size()const;
class Rang{};
class Cref;
Cref operator[](int i);
char operator[](int i)const;
};
class String::Cref{
friend class String;
String& s;
int i;
Cref(String& ss,int ii):s(ss),i(ii){}
public:
operator char(){return s.read(i);}
void operator=(char c){s.write(i,c);}
};
}
//main.cpp
#include "myString.h"
namespace smr{
String::String()
{
rep=new Srep(0,"");
}
String::String(const char* s)
{
rep=new Srep(strlen(s),s);
}
String::String(const String& x)
{
x.rep->n++;
rep=x.rep;
}
String& String::operator=(const String& x)
{
x.rep->n++;
if(--rep->n==0) delete rep;
rep=x.rep;
return *this;
}
String& String::operator=(const char* s)
{
if(rep->n==1)
rep->assign(strlen(s),s);
else{
rep->n--;
rep=new Srep(strlen(s),s);
}
return *this;
}
String::~String()
{
if(--rep->n==0) delete rep;
}
inline void String::check(int i)const
{if(i<0 || rep->sz<=i) throw Rang();}
inline char String::read(int i)const
{return rep->s[i];}
inline void String::write(int i,char c)
{rep=rep->get_own_copy();rep->s[i]=c;}
inline int String::size()const{return rep->sz;}
inline String::Cref String::operator[](int i) //////////编译的时候这个不能生成
{check(i);return String::Cref(*this,i);}
inline char String::operator[](int i)const
{check(i);return rep->s[i];}
}
int main()
{
smr::String s="smr";
s[0]='d';
return 0;
}
在我的机器上是没问题的,不知道你的是怎么回事了
我的也可以了,只要把这几个函数改为非在线函数:
Cref operator[](int i);
char read(int i)const;
void write(int i,char c);
如果不改的话,单独编译string类是可以通过的,但是加入了main以后就会提示下面错误:
无法解析的外部符号 "public: class smr::String::Cref __thiscall smr::String::operator[](int)"
无法解析的外部符号 "public: void __thiscall smr::String::write(int,char)"
谢谢yuyunliuhen了!!