可能重複:
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 –