C ++ Inner类不能访问外类的成员 [英] C++ Inner class not able to access Outer class's members

查看:161
本文介绍了C ++ Inner类不能访问外类的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

内部类可以访问私有变量吗?

Inner类访问外部类

我有一些简单的类嵌套他们可以与变量交互,没有额外的输入,但我的编译器给我一个错误。我如何允许他们进行交互,而不使用&时间作为函数输入或在Vect类中有变量&时间?

I have some simple classes nested so they can interact with a variable without extra inputs, yet my compiler gives me an error. How would I allow for them to interact without using &time as function input or having the variable &time inside of the Vect class?

我尝试使用相同的逻辑在那里你可以访问的数据只是在代码中,在同一个地方作为函数原型,而是包装在一个类。这对我使用的除了其他类以外的任何东西都很好。任何人都可以解释为什么?

I tried using the same logic where you can have data accessed that is just in the code, at the same place as function prototypes, but is instead wrapped in a class. This works fine for anything I've used except other classes. Can anyone explain why?

我已经标记了使用有问题的时间变量与注释行像定义之前的那些地方。

I have marked the places that use the problematic time variable with comment lines like the one before the define.

/*********/
#define MAX_POLY 3

class Container
{
public:
    Container(void);
    ~Container(void);

    float time;/*********/
    class Vect
    {
        float P[MAX_POLY],iT;
    public:
        Vect(void){iT = 0.0f;P = {0,0,0};}
        ~Vect(void);

        float GetPoly(int n){return P[n];}
        float Render(void)
        {
            float t = time - iT;/*********/
            float temp[2] = {0,0};
            for(int n=0;n<MAX_POLY;n++)
            {
                temp[0] = P[n];
                for(int m=0;m<n;m++)
                    temp[0] *= t;
                temp[1] += temp[0];
            }
            return temp[1];
        }
        void SetPoly(int n,float f)
        {
            float t = time-iT;/*********/
            P[0] = P[2]*t*t + P[1]*t + P[0];
            P[1] = 2*P[2]*t + P[1];
            //P[2] = P[2];
            P[n] = f;
            iT = time;/*********/
        }
    }X;
};

int main()
{
    Container Shell;
    Shell.X.SetPoly(0,5);
    Shell.X.SetPoly(1,10);
    Shell.X.SetPoly(2,-1);
    for(int n=0;n<10;n++)
    {
        Shell.time = (float)n;
        cout << n << " " << Shell.X.Render() << endl;
    }
    system("pause");
    return 0;
}


推荐答案

(我设法弄清楚,即使你没有发布实际的错误,请在未来做),是你实际上没有实例 > Container 类中 Vect 类的函数。你应该考虑这里的设计,但是为了快速解决它(和dirty)你可以添加一个函数,在子类中设置一个 Container 实例: / p>

The reason you get an error (I managed to figure it out even though you didn't post the actual error, please do that in the future), is that you actually don't have an instance of the Container class inside the functions of the Vect class. You should probably think about the design here, but to solve it quickly (and "dirty") you could add a function which sets a Container instance in the sub-class:

class Container
{
    ...

    class Vect
    {
        Container *container;

    public:
        void SetContainer(Container &container)
            { this->container = &container; }

        float Render(void)
            {
                float T = container->time - iT;
                ...
            }

        ...
    } X;
};

int main()
{
    Container Shell;
    Shell.X.SetContainer(Shell);
    Shell.X.SetPoly(0,5);
    ...
}

一个更好的方法是使用构造函数来设置对父对象的引用(感谢juanchopanza的想法):

A better way is to set a reference to the parent object (thanks to juanchopanza for the idea) using the constructors:

class Container
{
    ...

    Container()
        : X(*this)
        { ... }

    class Vect
    {
        Container& container;

    public:
        Vect(Container& cont)
            : container(cont)
            { }

        float Render(void)
            {
                float T = container.time - iT;
                ...
            }

        ...
    } X;
};

我仍然认为这是一种肮脏的解决方案(但不像我的第一个脏)你应该考虑改变设计。

I still think it's kind of a dirty solution (but not as dirty as my first), and that you should think about changing the design instead.

这篇关于C ++ Inner类不能访问外类的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆