这段程序并不完整,主要帮我看下struct Srep这个,
我的编译器里报错就是在这:error C2027: 使用了未定义类型“String::Srep
但我再类声明后面已经定义了, 这让我十分不明白谁能给我解释下。
#include
#include
#include
using namespace std;
class String{
struct Srep;
Srep *rep;
public:
class Cref;
class Range{};
public:
String();
String(const char*);
String(const String &);
String& operator=(const char *);
String& operator=(const String&);
~String();
public:
void check(int i)const {if(iszs(i);}
void write(int i,char c){rep=rep->get_own_copy();rep->s(i)=c;}
Cref operator[](int i){check(i);return Cref(*this,i);}
char operator[](int i)const{check(i); return rep->s(i);}
int size()const {return rep->sz;}
};
struct String::Srep{
char *s;
int sz;
int n;
Srep(int nsz, const char* p)
{
n = 1;
sz = nsz;
s = new char[sz+1];
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];
}
strcpy(s,p);
}
private:
Srep(const Srep&);
Srep& operator=(const Srep&);
};
String ::String()
{
rep = new Srep(0,"");
}
String::String(const String& x)
{
x.rep->n++;
rep = x.rep;
}
String::~String()
{
if(--rep->n == 0)delete rep;
}
String& String::operator=(const String& x)
{
x.rep->n++;
if(--rep->n == 0)delete rep;
rep = x.rep;
return *this;
}
///////////////////////////////////////////
String::String(const char *s)
{
rep = new Srep(strlen(s),s);
}
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;
}
////////////////////////////////////
int hask(const String& s)
{
int h = s.read(0);
const int max = s.size();
for(int i=1; i>1;
return h;
}
}
|