c++ - 当我定义一个指向类的指针时,错误 :expected constructor, 析构函数,或者在 '*' token 发生之前进行类型转换

标签 c++

int buffer[gSizeOfGrid][gSizeOfGrid];
CList *currentCList;
Grid *currentGrid; //this line get the error
int aroundinfo[4];
//other functions are not revelant maybe? They don't
//use currentGrid.

类 Grid 有标题:

class Grid
{
public:
    Grid();
    //create default cList object for the grid .
    Grid(CList*);
    //create cList and bind existing clist object.
    bool rebind(CList*);
    //rebind cList.
    ~Grid();
    void init(int ants,int bugs);
    //init a grid with ants and bugs according to parameter.
    void GetSnapshot();
    //synchronize grid buffer with clist.
    void Step();
    //make the world go on!
    bool spawn(Creature&,Position);
    //create a creature on the grid.
    //if success, return true.
    //if there are already another creature return false.
    void move(Position,int);
    void eat(Position,int);
    void breed(Position,int);
    void destroy(Position);
private:
    CList* theList;
};

Clist 类看起来一样,但在声明中没问题:

using namespace std;
class CList
{
public:
    CList();
    int CreateObject(Creature&,Position);
    Creature& GetObject(int);
    bool RemoveObject(Position);
    bool RemoveObject(int);
    int getType(Position);
    void cleanup();
    vector<Creature*> list;
    int size();
};

为什么 Grid* 声明出错,而 CList* 没问题?谢谢!

最佳答案

这是因为 class Grid 对指针 currentGrid 不可见。只需执行以下操作即可;

class Grid;
Grid *currentGrid;

我假设您可能不希望在此声明之前#include Grid header ,因为Grid 本身正在使用CList 。最好的方法应该是在此声明之前包含所有 header 。

关于c++ - 当我定义一个指向类的指针时,错误 :expected constructor, 析构函数,或者在 '*' token 发生之前进行类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5855808/

相关文章:

c++ - 使用boost asio从串口读取最新数据

c++ - 如何迭代boost R-tree?

c++ - 定义变量时出现奇怪的错误

c++ - HDF5 - 通过代码为组创建属性

c++ - std::fstream 应该如何工作?

c++ - SQL 解析器库 - 从查询中获取表名

c++ - 在运行时使用模板参数实例化类(C++)?

创建内存分配器时出现 C++ 继承错误

c++ - 如何编写在输入到达内核之前拦截输入的程序?

c++ - 按值传递和 move ,或两种方法