- 论坛徽章:
- 0
|
《TC++PL》第11章最后实现了一个String类,其中用到了类内部定义的类和结构。大致结构类似于下面的小程序:
#include <stdio.h>
class T
{
public:
T(int num) { s = new S(num); }
void print() { printf("%d\n", s -> number); }
private:
struct S;
S *s;
};
struct T::S
{
int number;
S(int num) : number(num) {}
private:
S(const S&);
S& operator=(const S&);
};
int main()
{
T t(3);
t.print();
return 0;
} |
出现下面错误:
6:error:invalid use of incomplete type 'struct T::S'
9:error:forward declaration of 'struct T:S:'
7:error:invalid use of incomplete type 'struct T::S'
9:error:forward declaration of 'struct T:S:'
如果把结构体S的内容在类T的声明中展开,就可以编译通过,但是《TC++PL》的程序的确是像上面的程序那样用内部类的,是因为编译器不支持书上这种语法的原因么? |
|