不能使用循环包含转发声明

cannot forward declare with circular includes

本文关键字:转发 声明 包含 循环 不能      更新时间:2023-10-16

我一直在寻找解决这个问题的方法已经有一段时间了,这里的每个答案都对不包括头文件有一些变化,但我似乎已经签约了愚蠢并且无法弄清楚......

我有C++类creature的基础,应该在ladybirdaphid中实现,但我不断收到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",以下是您将按顺序包含的文件:

  1. Creature.H(定义包含警卫符号CREATURE_H_
  2. Board.H(来自Creature.H)
  3. 单元格.H
  4. (来自Board.H)
  5. Aphid.H(来自cell.h,由于包含警卫而跳过)
  6. Ladybird.H(来自cell.h)
  7. 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"