震撼:invalid use of undefined type,该怎么解决
震撼:invalid use of undefined type
有下面的代码:
using namespace std;
class String
{
struct Srep; //representation.
Srep* rep;
public:
class CRef; //reference to char
class Range{}; //for exceptions
String(); // x= " "
String( const char* ); //x= "abc "
String( const String& ); //x=other_string
String& operator=(const char* );
String& operator=(const String&);
~String();
void check( int i ) const
{
if( i < 0 || rep-> sz <= i ) // <-这一句编译报错: invalid use of undefined type `struct String::Srep '
{
throw Range();
}
}
};
struct String::Srep
{
char* s; //pointer to elements
int sz; //number of characters
int n; //reference count;
Srep( int nsz, const char* p )
{
n = 1;
sz = nsz;
s = new char[sz+1]; //add space for teminitor
strcpy(s,p);
}
~Srep() { delete[] s; }
Srep* get_own_copy() //clone if necessary
{
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: //prevent copying
有下面的代码:
using namespace std;
class String
{
struct Srep; //representation.
Srep* rep;
public:
class CRef; //reference to char
class Range{}; //for exceptions
String(); // x= " "
String( const char* ); //x= "abc "
String( const String& ); //x=other_string
String& operator=(const char* );
String& operator=(const String&);
~String();
void check( int i ) const
{
if( i < 0 || rep-> sz <= i ) // <-这一句编译报错: invalid use of undefined type `struct String::Srep '
{
throw Range();
}
}
};
struct String::Srep
{
char* s; //pointer to elements
int sz; //number of characters
int n; //reference count;
Srep( int nsz, const char* p )
{
n = 1;
sz = nsz;
s = new char[sz+1]; //add space for teminitor
strcpy(s,p);
}
~Srep() { delete[] s; }
Srep* get_own_copy() //clone if necessary
{
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: //prevent copying