【发布时间】:2015-03-07 00:16:21
【问题描述】:
我一直在寻找这个问题的解决方案一段时间,这里的每个答案都有一些不包括头文件的变化,但我似乎已经签约愚蠢并且无法弄清楚......
我有基础 C++ 类 creature,应该在 ladybird 和 aphid 中实现,但我不断收到 expected class-name before ‘{’ token 错误。
生物.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
蚜虫.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
【问题讨论】:
-
你不能从不完整的类型派生 - 你需要那个标题。此外,我在您发布的代码中没有看到任何循环包含。
-
cell.h是否包含aphid.h和ladybird.h?在这种情况下,由于creature.h包含cell.h,因此您在声明它之前是从Creature 继承的,并且您不能转发声明您从中继承的类。 -
@LogicStuff 你指的是我试图在
Aphid和Ladybird中转发声明Creature? -
请发布 board.h 和 cell.h,或者至少是这些文件的包含部分
-
invalid use of incomplete type class 'Creature’- 显然,您转发了Creature。查看其他评论并检查board.h和cell.h是否包含一些继承自Creature的类。