error in "The C++ programming language"?

chapter 11.12, a string class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class String {
    struct Srep;
    Srep* rep;
public:
    class Range { };
    //...

    void check(int i) const { if(i<0 || i>=rep->sz) throw Range(); }
    //...
};

struct String::Srep {
    char* s;
    int sz;
    //...
};

int main()
{
}

Biuld msg :
error:invalid use of undefined type 'struct String::Srep'
...

why Dr.Stroustrup would write like this?
Last edited on
That's perfectly valid C++ according to me, VC++, and MinGW.
Last edited on
You shouldn't use String::rep before you've actually declared it. So String::check() should be defined after the declaration of Srep.

You've used a pointer forward declaration, which is sufficient to define the layout of String, but you can't use it if you don't know what is is.
Of course. I commented that line. I guess I should have left it the way it was.
Topic archived. No new replies allowed.