简体   繁体   中英

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?

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

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.

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 . So to solve the problem you remove the inclusion of Alien.hpp in Laser.hpp , and add a declaration of the Alien class:

class Alien;

This is because of recursive header file including. Consider, laser.h includes alien.h which, once again, includes 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; , which is sufficient in your case because you use only a pointer to Alien class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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