模板和嵌套类

时间:2017-10-29 14:02:44

标签: c++ templates nested

我有一些嵌套类和struct

的类
template<typename Str>
class DeferredString {
private:
//  Representation of a shared string
    struct StringRep;
    StringRep* _rep;
//  Proxy class for a character in the string
    class CharProxy;
public: 
    DeferredString();
    DeferredString(const string&);
    DeferredString(const DeferredString<Str>&);
    DeferredString(const char*);
    ~DeferredString();
    DeferredString<Str>& operator=( const DeferredString<Str>&);
    DeferredString<Str>& operator=( const char* );
    void check(size_t i) const;
    char read(int i) const;
    void write (int i, char c);
    CharProxy operator[](int i);
    char operator[](int i) const;
    int length()const;
};

这是嵌套类和结构的实现:

template<typename Str>
class DeferredString<Str>::CharProxy
{
    friend class DeferredString<Str>;
private:
//  String to which belongs proxy
    DeferredString<Str> & _proxyship;
//  Character substituted by a proxy
    int _index;
    CharProxy(DeferredString<Str>& ds, int i);

public:
    const char* operator&() const;
    char* operator&();
    operator char() const;
    operator char&();
    CharProxy& operator=(char c);
    ostream& operator<<(ostream & os);
};

template<typename Str>
struct DeferredString<Str>::StringRep
{
    //  pointer to the allocation og a string
    Str* _allocator;
    //  number of characters in the string
    size_t _len;
    //  how many oblects use this representation
    int _refCounter;
    //  if the string may be sharable
    bool _shareable;

    StringRep(size_t, const char*);
    StringRep(size_t, const string&);
    ~StringRep();
    //  pseudo copy constructor
    StringRep* getOwnCopy();
    //  pseudo assignment
    void assign(size_t, const char*);
    void makeShareable() { _shareable = true; }
    void makeUnshareable() { _shareable = false; }
    bool isShareable() const { return _shareable; }
private:
    //  Wil never be implemented
    StringRep(const StringRep&);
    StringRep& operator= (const StringRep&);
};

在我运行程序后,我在实现中遇到了很多错误。例如:

template<typename Str>
DeferredString<Str>::CharProxy DeferredString<Str>::operator[](int i)
{
    check(i);
    //  A proxy will be returned instead of a character
    return CharProxy(*this, i);
}

编译器说 错误C2061语法错误:标识符&#34; CharProxy&#34; 这么多。我不知道这里的问题是什么。你能救我吗?

0 个答案:

没有答案