相关疑难解决方法(0)

C++静态成员函数和变量

我正在通过进行小型机器人模拟来学习C++,而我在类中的静态成员函数方面遇到了麻烦.

我的环境类定义如下:

class Environment {
    private:
        int numOfRobots;
        int numOfObstacles;

        static void display(); // Displays all initialized objects on the screen

    public:
        Robot *robots;
        Obstacle *obstacles;

        // constructor
        Environment();

        static void processKeySpecialUp(int, int, int); // Processes the keyboard events
};
Run Code Online (Sandbox Code Playgroud)

然后在构造函数中我初始化机器人和障碍物,如下所示:

numOfRobots = 1; // How many robots to draw
numOfObstacles = 1;
robots = new Robot[numOfRobots];
obstacles = new Obstacle[numOfObstacles];
Run Code Online (Sandbox Code Playgroud)

以下是使用这些变量的静态函数示例:

void Environment::display(void) {
    // Draw all robots
    for (int i=0; i<numOfRobots; i++) {
        robots[i].draw();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译时,我会收到类似的错误消息

error: …
Run Code Online (Sandbox Code Playgroud)

c++

11
推荐指数
1
解决办法
4万
查看次数

标签 统计

c++ ×1