可能重复:
Can inner classes access private variables?
Inner class accessing outer classC++内部类不能访问外部类的成员
我有嵌套,使他们能够有没有额外的输入变量相互作用的一些简单的类,但我的编译器给我一个错误。我如何允许它们在不使用&时间作为函数输入或具有变量&时间在Vect类中进行交互?
我试过使用相同的逻辑,您可以在代码中访问数据,但只能在函数原型的相同位置访问,而不是包装在类中。这适用于我除了其他类以外的任何事情。谁能解释为什么?
我已经标记了使用有问题时间变量的地方,并在定义之前使用了注释行。
/*********/
#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;
}
你的编译器给你_what_错误? –
为什么嵌套类? –
看看这个:http://stackoverflow.com/questions/486099/can-inner-classes-access-private-variables –