2012-09-23 82 views
-1

我得到了我的主类游戏C++类艾策斯阵列阵列从其他类

Galaxian.h:

class Galaxian{ 
public: 
    void update(); 
}; 

Galaxian.cpp:

这是我的问题:我想从游戏类访问galaxians数组,但我不知道如何!当我尝试游戏:: galaxians时,出现错误“非静态成员参考必须是相对于特定对象的”

我想完成的是我可以循环槽数组并更改每个键的值它。

我该怎么做?

回答

1

static成员绑定到一个类的实例,而不是类本身。这是通用的面向对象,不是C++特有的。让你无论是绑定访问对象,或成员到类:

Game g;  //create an object of the class 
g.galaxians; //access the member through the object 

class Game{ 
public: 
    static Galaxian galaxians[6][10]; //bind the member to the class 
}; 

//... 

Game::galaxians; //access it through the class 

你选择哪一个取决于你的逻辑。

2

这是因为galaxians成员是实例成员,而不是类(即非静态)成员。您应该(1)在需要访问galaxians的位置提供Game的实例,或者(2)使galaxians成为静态成员。

如果您决定第一种方式,考虑制作Game a 单身人士;如果您决定采用第二种方式,请不要忘记在cpp文件中定义您的galaxians数组,除了在头文件中声明它static

1

您需要访问的Game一个实例:

Game g; 
g.galaxians[3][4] = ....;