代码之家  ›  专栏  ›  技术社区  ›  Dan Jones

C类之间的TypeDef错误++

  •  -3
  • Dan Jones  · 技术社区  · 9 年前

    我的人工智能。cpp需要敌人的价值观。cpp能够在人工智能中处理其运动等。cpp I持有指向位置、速度和敌人所面临方向的枚举等值的指针。

    下面是该错误的工作示例。

    ///////MAIN.H/////////////
    
    #ifndef  _MAIN_H_
    #define _MAIN_H_
    
    
    
    #endif // ! _MAIN_H_
    
    ///////////////////////////
    
    ////////MAIN.CPP///////////
    
    #include "AI.h"
    #include "Enemy.h"
    
    int main(int argc, char* args[])
    {
        Enemy enemy; 
    
    }
    
    //////////////////////////////
    
    //////////ENEMY.H///////////////
    
    #ifndef _ENEMY_H_
    #define _ENEMY_H_ 
    
    #include "AI.h"
    
    class Enemy 
    {
    
    public: 
    
        Enemy();
    
        enum Facing
        {
            LEFT = 0,
            RIGHT
        };
    
    
    
    protected:
    
        AI* EnemiesAI; 
        //try this as a pointer
        Facing EnemyDirection;
    
    };
    
    
    #endif
    
    //////////////////////////////////
    
    ///////////ENEMY.CPP////////////////
    
    #include "Enemy.h"
    
    Enemy::Enemy()
    {
        EnemiesAI = new AI; 
    
        EnemiesAI->LinkEnemyToAI(&EnemyDirection);
    }
    
    ////////////////////////////////////
    
    /////////////////AI.H//////////////
    
    #ifndef _AI_H_
    #define _AI_H_
    
    #include "Enemy.h"
    
    class AI
    {
    public: 
    
        /*this needs to be a pointer, otherwise I have to pass the 
        value into AI on every update*/
        typedef Enemy::Facing *ThisFacing; //Error 3
    
        void LinkEnemyToAI(ThisFacing facing);
    
    private: 
        //This is a pointer to a Enemy::Facing object
        ThisFacing mFacing; 
    
    
    };
    
    #endif
    
    ///////////////////////////////////////
    
    ////////////////AI.CPP/////////////////
    
    #include "AI.h"
    
    void AI::LinkEnemyToAI(ThisFacing facing)
    {
        mFacing = facing;
    }
    
    ////////////////////////////////////////
    

    错误C2653“敌人”:不是类或命名空间名称ExampleOfTypeDefError c:\dev\projects\ExampleOfTypeDefError\ExampleOfTypeDefError\ai。h 11

    错误C4430缺少类型说明符-假设为int。注意:C++不支持默认的int-ExampleOfTypeDefError C:\dev\projects\ExampleOfTypeDefError\ExampleOfTypeDefError\ai。h 11

    错误C3646“mFacing”:未知重写说明符ExampleOfTypeDefError c:\dev\projects\ExampleOfTypeDefError\ExampleOfTypeDefError\ai。h 17

    严重性代码描述项目文件行

    严重性代码描述项目文件行 错误C2061语法错误:标识符“ThisFacing”ExampleOfTypeDefError c:\dev\projects\ExampleOfTypeDefError\ExampleOfTypeDefError\ai。h 13

    严重性代码描述项目文件行 错误C2238“;”之前的意外令牌示例TypeDeferror c:\dev\projects\ExampleOfTypeDefError\ExampleOfTypeDefError\fy。h 23

    1 回复  |  直到 9 年前
        1
  •  0
  •   Some programmer dude    9 年前

    在这里使用include-guard并没有真正的帮助,因为 Enemy.h AI.h 这取决于 敌人。h

    你必须打破这条链条,最简单的方法是通过一种叫做 提前声明

    如果你仔细观察 敌人。h 头文件,它使用的所有 AI 这意味着你不需要对 人工智能 人工智能 存在。类似于

    #ifndef ENEMY_H
    #define ENEMY_H
    
    class AI;  // Forward declaration of the AI class    
    
    class Enemy 
    {
    public: 
        Enemy();
    
        enum Facing
        {
            LEFT = 0,
            RIGHT
        };
    
    protected:
        AI* EnemiesAI; 
        //try this as a pointer
        Facing EnemyDirection;
    };
    
    #endif // ENEMY_H
    

    现在在 Enemy.cpp #include "AI.h" 因为这里需要类的完整定义。

    这种转发声明在 AI.h 头文件,因为在其中使用枚举 Enemy::Facing 敌人。h 不再包括 不再存在循环依赖。

    还要注意的是,我将标题保护宏更改为没有前导下划线,因为在所有作用域中保留了所有前导下划线跟随符为大写字母的符号。参见示例。 this question and its answers 了解更多详细信息。

    推荐文章