我一直在寻找这个问题的解决方案一段时间了,这里的每个答案都有一些变化,不包括头文件,但我似乎已经收缩了愚蠢而无法弄明白...
我有基础C ++类creature
,应该在ladybird
和aphid
中实现,但我不断收到expected class-name before ‘{’ token
错误。
creature.h
#ifndef CREATURE_H_
#define CREATURE_H_
#include <iostream>
#include "board.h"
#include "cell.h"
class Creature {
private:
int x, y;
protected:
Creature(int x, int y);
public:
int getX();
int getY();
//virtual bool willMove() = 0;
//virtual void doMove(Board *board) = 0;
//std::ostream& operator<<(std::ostream &out, Creature &cCreature);
};
#endif
aphid.h:
#ifndef APHID_H_
#define APHID_H_
#include "creature.h"
class Aphid : public Creature {
public:
Aphid(int x, int y);
static float moveProbability;
static float killProbabllity;
static float killModifier;
static float matingProbability;
//virtual bool willMove();
//virtual void doMove(Board *board);
};
#endif
ladybird.h:
#ifndef LADYBIRD_H_
#define LADYBIRD_H_
#include "creature.h"
class Ladybird : public Creature {
public:
Ladybird(int x, int y);
static float moveProbability;
static float directionProbability;
static float killProbabllity;
static float matingProbability;
//virtual bool willMove();
//virtual void doMove(Board *board);
};
#endif
完整堆栈跟踪
g++ src/*.cpp -o out/main.o -Wall -std=c++11 && ./out/main.o
In file included from src/cell.h:8:0,
from src/board.h:4,
from src/creature.h:5,
from src/aphid.h:4,
from src/aphid.cpp:2:
src/ladybird.h:6:34: error: expected class-name before ‘{’ token
class Ladybird : public Creature {
^
src/cell.cpp: In member function ‘void Cell::simulateMove(Board*)’:
src/cell.cpp:44:19: error: ‘class Aphid’ has no member named ‘willMove’
if((*it)->willMove()){
^
src/cell.cpp:45:18: error: ‘class Aphid’ has no member named ‘doMove’
(*it)->doMove(newBoard);
^
In file included from src/cell.h:7:0,
from src/board.h:4,
from src/creature.h:5,
from src/creature.cpp:1:
src/aphid.h:6:31: error: expected class-name before ‘{’ token
class Aphid : public Creature {
^
In file included from src/cell.h:8:0,
from src/board.h:4,
from src/creature.h:5,
from src/creature.cpp:1:
src/ladybird.h:6:34: error: expected class-name before ‘{’ token
class Ladybird : public Creature {
^
In file included from src/cell.h:7:0,
from src/board.h:4,
from src/creature.h:5,
from src/ladybird.h:4,
from src/ladybird.cpp:2:
src/aphid.h:6:31: error: expected class-name before ‘{’ token
class Aphid : public Creature {
^
我已尝试声明Creature
课程,但后来我invalid use of incomplete type ‘class Creature’
board.h:
#ifndef BOARD_H_
#define BOARD_H_
#include "cell.h"
#include "creature.h"
class Board{
private:
Cell ***board; // A 2D ARRAY OF POINTERS, "what if i just add more stars??!" -- Owen G
int width;
int height;
public:
Board(int width, int height);
void populate();
Cell* getCell(int x, int y);
void print();
void simulateMove(Board *board);
};
#endif
cell.h:
#ifndef CELL_H_
#define CELL_H_
#include <iostream>
#include <list>
#include "aphid.h"
#include "ladybird.h"
#include "board.h"
class Board;
class Aphid;
class Ladybird;
class Cell{
private:
int x, y;
std::list<Aphid*> aphids;
std::list<Ladybird*> ladybirds;
public:
Cell(int x, int y);
void addCreature(Aphid *creature);
void addCreature(Ladybird *creature);
int getAphidCount();
int getLadybirdCount();
void simulateMove(Board *nextGenerationBoard);
};
#endif
答案 0 :(得分:2)
在cell.h中,你有:
#include "aphid.h" #include "ladybird.h" #include "board.h" class Board; class Aphid; class Ladybird;
这没有意义。首先包括整个类定义,然后再次声明类。只需删除#include
行:
class Board;
class Aphid;
class Ladybird;
在board.h中,你有:
#include "cell.h" #include "creature.h"
然后是以下类定义:
class Board{ private: Cell ***board; // A 2D ARRAY OF POINTERS, "what if i just add more stars??!" -- Owen G int width; int height; public: Board(int width, int height); void populate(); Cell* getCell(int x, int y); void print(); void simulateMove(Board *board); };
Creature
未在任何地方使用,Cell
仅用于指针。您也可以安全地删除这些#include
行,并使用Cell
的前向声明替换它们。
顺便说一句:您应该尽快摆脱Cell***
并使用std::vector
代替。
答案 1 :(得分:1)
您的文件中存在循环#include依赖关系 假设您首先在一些.cpp文件中包含“aphid.h”,以下是您要按顺序包含的文件:
creature.h(来自ladybird.h,跳过因为包含警卫)
- &GT; ladybird.h将无法解析,因为当Ladybird类声明发生时,尚未定义Creature:
class Ladybird : public Creature { ... };
为了解决这个问题,你应该从文件cell.h中删除以下行:
#include "aphid.h"
#include "ladybird.h"
这编译很好,因为你在cell.h中使用这些类的唯一方法是通过指向它们的指针,这不需要完整的声明,并打破循环包含。
出于同样的原因,您可能还应从该文件中删除#include "board.h"
。当你在它时,你也可以从board.h文件中删除#include "cell.h"
。