不能使用循环包含转发声明
cannot forward declare with circular includes
我一直在寻找解决这个问题的方法已经有一段时间了,这里的每个答案都对不包括头文件有一些变化,但我似乎已经签约了愚蠢并且无法弄清楚......
我有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
蚜虫:
#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
瓢虫:
#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’
板.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
单元格.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 中,您有:
#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
。
您的文件中有一个循环 #include 依赖项。
假设您首先在某些.cpp文件中包含"aphid.h",以下是您将按顺序包含的文件:
- Creature.H(定义包含警卫符号CREATURE_H_)
- Board.H(来自Creature.H) 单元格.H
- (来自Board.H)
- Aphid.H(来自cell.h,由于包含警卫而跳过)
- Ladybird.H(来自cell.h)
Creature.H(来自Ladybird.H,因为包括警卫而跳过)
-> 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"
。
- 如何在 C++ 中转发声明 std::set?
- C++使用默认模板参数键入别名和转发声明
- 如何转发声明枚举?
- 使用函数指针转发声明作为 lamba 声明
- 如何将枚举类转发声明为模板化类的内部类?
- 转发声明在命名空间中不起作用的替代方法
- 如何正确转发声明结构"using XXX"?
- 在类内和类外的定义处执行类转发声明是否有区别
- 为什么转发声明的好友类不能在类中引用?
- 如何访问转发声明类的成员
- 仅在标头开发中转发声明
- 无法转发声明C typedef结构-正在查找其他建议
- boost::hana 转发声明标头的用例是什么?
- 循环包含依赖项/转发声明
- c++ 使用复杂的依赖项获取正确的转发声明
- 如何转发声明依赖于变量定义的类,而变体定义又依赖于模板化类?
- 有没有办法转发声明命名空间或只是提前使其可见
- 有没有办法在不引用其模板类型的情况下转发声明指向类的指针
- 我如何转发声明boost ::精神统治
- 标题文件中大规模转发声明类别的风险是什么?