无法访问类的成员

Bar*_*ski 2 c++ class

我有一点问题,我可能错误地包含了类文件,因为我无法访问敌人类的成员.我究竟做错了什么?我的班级cpp

#include "classes.h"

class Enemy
{
 bool alive;
 double posX,posY;
 int enemyNum;
 int animframe;
public:
Enemy(int col,int row)
{
    animframe = rand() % 2;
    posX = col*50;
    posY = row*50;
}

Enemy()
{

}
void destroy()
{
    alive = 0;
}
void setposX(double x)
{x = posX;}
void setposY(double y)
{y = posY;}
};
Run Code Online (Sandbox Code Playgroud)

我的标题:

class Enemy;
Run Code Online (Sandbox Code Playgroud)

我的主要:

#include "classes.h"
Enemy alien;
int main()
{
    alien. // this is where intelisense tells me there are no members
}
Run Code Online (Sandbox Code Playgroud)

Vla*_*lad 6

您的主文件只会看到您在标题中写的内容,这Enemy是一个类.通常,您将在头文件中使用字段和方法签名声明整个类,并在.cpp文件中提供实现.

classes.h:

#ifndef _CLASSES_H_
#define _CLASSES_H_
class Enemy
{
    bool alive;
    double posX,posY;
    int enemyNum;
    int animframe;
public:
    Enemy(int col,int row);
    Enemy();
    void destroy();
    void setposX(double x);
    void setposY(double y);
};
#endif
Run Code Online (Sandbox Code Playgroud)

classes.cpp:

#include "classes.h"
//....
void Enemy::destroy(){
    //....
}
//....
Run Code Online (Sandbox Code Playgroud)