gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:20:57 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com