谁能帮我看下这++段程序到底出错再那里?

[复制链接]
查看11 | 回复2 | 2011-5-21 23:28:21 | 显示全部楼层 |阅读模式
这段程序并不完整,主要帮我看下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;
        }
}

回复

使用道具 举报

千问 | 2011-5-21 23:28:21 | 显示全部楼层
结构体在类后面定义,当然找不着了。另外,对于结构体的定义,看着乖乖的,还没有见过类似这样的写法。struct String::Srep{}为什么要有个String::?去掉这里的String::,把结构体放在类前面,或者在类前面加上一句前向声明:struct Srep;
回复

使用道具 举报

千问 | 2011-5-21 23:28:21 | 显示全部楼层
要把函数在类的定义 里面声明 ,不然后面写的没意义 的。。。
回复

使用道具 举报

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

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行