简体   繁体   English

函数参数中未声明的标识符

[英]Undeclared identifier in function parameter

I'm getting an undeclared identifier error when compiling my little game that I'm working on for education purposes and I can't for the world understand why and where it is coming from. 编译出于教育目的而工作的小游戏时,出现未声明的标识符错误,并且我无法为世界所知。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Why does the following line from my "laser header" file generate undeclared identifier error C2065? 为什么我的“激光头”文件中的以下行会生成未声明的标识符错误C2065?

bool hit(std::vector<Alien*> aliens);

Here is the full header file: 这是完整的头文件:

#ifndef LASER_HPP_
#define LASER_HPP_

#include "Ship.h"
#include "Alien.h"
#include "Vector.h"

class Laser : public Sprite {
    private :
        bool armed;
        int reloadTime, width, height, radius;

        std::vector<Vector> bullets;

        bool circleCollision(int x, int y, Ship* ship);
        void reload();
    public:
        enum Type {
            RED,
            BLUE
        };

        Laser(Type type);

        bool hit(std::vector<Alien*> aliens);
        bool hit(Ship* ship);
        void shoot(int x, int y, int dy);
        void update();
        void draw();
};

#endif LASER_HPP_

And also the header file for Alien: 还有外星人的头文件:

#ifndef ALIEN_HPP_
#define ALIEN_HPP_

#include "Laser.h"
#include "Ship.h"

class Alien : public Ship {
    private:
        int nextX, laserSpeed, shotDelay;
        bool attacking;
        time_t lastShot;

        bool targetReached();
        bool timeToShoot();
        bool enteringStage();

        void shoot();
        void move();
        void init();
    public:
        Alien();
        Alien(const char* filename);
        Laser* laser;

        void setNextTarget();
        void update();
        void attack();
        void die();
};

#endif ALIEN_HPP_

I'm using VisualStudio 2012 :S 我正在使用VisualStudio 2012:S

Error: 错误:

 error C2065: 'Alien' : undeclared identifier

It's a classic example of circular inclusion dependency. 这是循环包含依赖的经典示例。 The Laser.hpp file needs the Alien.hpp file which needs the Laser.hpp file and so on. Laser.hpp文件需要Alien.hpp文件,而Laser.hpp文件需要Laser.hpp文件,依此类推。

In this case, Laser.hpp doesn't really need the Alien.hpp file at all, it just need to know that there is a class named Alien . 在这种情况下, Laser.hpp根本根本不需要Alien.hpp文件,只需要知道有一个名为Alien的类即可。 So to solve the problem you remove the inclusion of Alien.hpp in Laser.hpp , and add a declaration of the Alien class: 因此,要解决该问题,请删除Alien.hpp中包含Alien.hppLaser.hpp ,并添加Alien类的声明

class Alien;

This is because of recursive header file including. 这是因为包含递归头文件。 Consider, laser.h includes alien.h which, once again, includes laser.h . 考虑一下, laser.h包含alien.h ,而后者又包含了laser.h This thing, the compiler could not do, even w/ header guards at place. 这件事,编译器无法做到,甚至没有标头保护。 What you need here is a forward declaration, instead of #include "Alien.h" add a declaration class Alien; 这里需要的是一个前向声明,而不是#include "Alien.h" ,而是添加声明class Alien; , which is sufficient in your case because you use only a pointer to Alien class. ,在您的情况下就足够了,因为您仅使用了指向Alien类的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM