【发布时间】:2016-04-09 08:05:25
【问题描述】:
我正在尝试用 C++ 编写一个简单的游戏,目前我的 Game_Window 类包含一个指向游戏对象的指针数组,如下所示:
class Game_Window {
private:
int width;
int height;
int num_objects;
public:
char** objects;
/* The rest of the class goes here */
}
在我的 Game_Window 类中,我想定义一个函数,对游戏窗口“objects”数组中保存的所有对象调用“print()”函数,如下所示。
void Game_Window::print_objects() {
for (int i = 0; i < num_objects; i++) {
(objects[i])->print(); /* THE PROBLEM IS HERE */
}
}
编译时出现以下错误:
game_window.cpp:29:15: error: member reference base type 'char' is not a structure or union
(objects[i])->print();
~~~~~~~~~~~~^ ~~~~~
1 error generated.
我游戏中的所有对象都有一个“print()”函数,所以我知道这不是问题所在。任何帮助将不胜感激。
【问题讨论】:
-
你有一个
char*指针数组,而不是游戏对象或你想要的任何东西。
标签: c++ arrays oop polymorphism member-functions