两个语法错误:错误C2143语法错误:缺少';' 在'*'之前

Rav*_*avo 1 c++

所以我有一些语法错误说:

Error C2143 syntax error: missing ';' before '* '  
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int  
Error C2238 unexpected token(s) preceding ';'  
Error C2143 syntax error: missing ';' before '*'  
Run Code Online (Sandbox Code Playgroud)

所有这些都在:

#pragma once  
#include "World.h"  
class Organism  
{  
protected:  
    int strength;
    int initiative;    
    int age, x, y;  
    char sign;  
    World *world;  //line that makes errors
public:  
    Organism(World*,int,int);
    virtual ~Organism();
    virtual void action() = 0;
    virtual void collision() = 0;
    virtual char getSign() = 0;
};
Run Code Online (Sandbox Code Playgroud)

我也有这些错误(是的,两次同样的错误):

Error   C2061   syntax error: identifier 'World'
Error   C2061   syntax error: identifier 'World'
Run Code Online (Sandbox Code Playgroud)

符合Organism(World*,int,int); (我不知道如何在StackOverflow上添加行号).什么可能导致这些问题?

这是World.h代码:

#pragma once
#include "Organism.h"
class World
{
    int height;
    int width;
    Organism*** organisms;
public:
    World();
    World(int, int);
    ~World();
    void DrawWorld();
    void NextRound();
};
Run Code Online (Sandbox Code Playgroud)

Som*_*ude 7

这是因为"Organism.h"头文件依赖于"World.h"头文件,这取决于"Organism.h"无穷大等等.这是一种所谓的循环依赖.

在你的情况下,它很容易打破,因为你所显示的头文件都不需要其他类的定义,只需要声明.

这意味着World.h头文件可能看起来像这样:

#pragma once
// Note: No #include directive here
class Organism;  // Forward-declaration of the class
class World
{
    int height;
    int width;
    Organism*** organisms;
public:
    World();
    World(int, int);
    ~World();
    void DrawWorld();
    void NextRound();
};
Run Code Online (Sandbox Code Playgroud)

使用Organism.h头文件也可以这样做.

使用这些类的源文件需要类的完整定义,因此它们需要包含两个头文件.