如何访问在另一个类中声明的类?

问题描述:

我有一个简单的课程:

class cup{
public:
     cup();
     void drinkwater(water *t); // error occurs here
private:
     class water{
     public:
          int litres;
     };
};

但是我收到一个错误,说'water'有没有办法访问类,而不必把水类外面?

However I get an error saying that 'water' has not been declared. Is there any way to access the class without having to put the water class outside?

向前声明 class water; $ c>在 drinkwater 的定义之上的私有部分,例如

Forward-declare class water; in a private section above the definition of drinkwater, like

class cup{
    class water;
public:
     cup();
     void drinkwater(water *t); // error occurs here
private:
     class water{
     public:
          int litres;
     };
};