将值从一个类传递到另一个类.单独的cpp文件.C++

Passing Values from one class to another. Separate cpp files . C++

本文关键字:单独 另一个 cpp C++ 文件 一个      更新时间:2023-10-16

我很难将值从一个CPP类文件传递到另一个CPP文件。

我一直在尝试大量不同的运算符来尝试传递值,我知道我遗漏了一些小东西,但我想不出其他什么可以做的,所以我在这里。

我正试图将player类中的位置x_posy_pos传递给我的bullet类,以便子弹从玩家的位置发射。我只是无法将值传递给项目符号类,其中包含了标头。

其中一些被评论掉了,现在我把它从同一个地方放了起来。

#include "bullet.h"
#include "player.h"
Bullet::Bullet()
{
    x_pos = 400;
    y_pos = 300;
    sprite = load_bitmap("bullet.bmp", 0);
}
Bullet::~Bullet()
{
    destroy_bitmap(sprite);
    destroy_sample(Music);
}
void Bullet::update()
{
    x_pos -= 0.5;
    y_pos -= 0.5;
    //Music = load_sample("shot.wav");
//  play_sample(Music,255,128,500,true);
    if(x_pos > SCREEN_W)
    {
        destroy_bitmap(sprite);
    }
    if(x_pos < 0)
    {
        destroy_bitmap(sprite);
    }
    if(y_pos > SCREEN_H)
    {
        destroy_bitmap(sprite);
    }
    if(y_pos < 0)
    {
        destroy_bitmap(sprite);
    }
}
void Bullet::draw(BITMAP* buff)
{
    //if(key[mouse_b&1])
        //   {
              masked_blit(sprite,buff,0,0,(x_pos),(y_pos),sprite->w,sprite->h);
             // y_pos --;
         //  }
}
//masked_blit(sprite,buff,0,0,(&player::getX),(&player::getY),sprite->w,sprite->h);

#include "player.h"
#include "bullet.h"
player::player()
{
    x_pos = 400;
    y_pos = 300;
    sprite = load_bitmap("player.bmp", 0);
    bulletSprite = load_bitmap("bullet.bmp", 0);
    spriteState = idle;

    rightWalk = 0;
    leftWalk = 0;
    upWalk = 0;
    downWalk = 0;
}
float player::getX()
{
    return x_pos;
}
float player::getY()
{
    return y_pos;
}

任何帮助都将不胜感激。

编辑包含的头文件。

Bullet.h

#include "Headers.h"

class Bullet
{
private:
    //float x_pos, y_pos; // this is the initial x and y position variable for the bullet
    float angle;
    BITMAP *sprite; // this is the sprite variable  
    SAMPLE *Music;

public:
    Bullet::Bullet();
    ~Bullet();
    int x,y;
    int x_pos, y_pos; // this is the initial x and y position variable for the bullet
    void draw(BITMAP* buff);
    void update();
    float setX(float x);
    float setY(float y);
};
player.h
#include "Headers.h"

class player
{
private:
    //double rightX, leftX; // this is the initial x and y position variable for the player
    BITMAP *sprite; // this is the sprite variable
    BITMAP *bulletSprite; // this is the sprite variable
    // put enemy sprite here.
    enum state{idle, up, right,down,left}; // this holds the movement for the player
    state spriteState;
    int leftWalk;
    int rightWalk;
    int upWalk;
    int downWalk;

    double bullet_direction;
    double bulletDirection();
public:
    player();
    ~player();
    double radians;
    double aimAngle();
    float x_pos, y_pos; // this is the initial x and y position variable for the player
    void mouseAngle(BITMAP* bullets);
    void hit();
    void update(BITMAP* buff);
    void draw(BITMAP* buff);
    float getX();
    float getY();
};

我知道我做错了什么。我玩了一个小时了。收到类似的消息。

错误非静态成员引用必须相对于特定对象

看看人们的评论,我认为我没有链接两个CPP文件,或者我没有设置玩家坐标的x位置。

在游戏中,当我按下鼠标按钮时,它从300400开始发射,而且只有这个位置。

我想把玩家的x和y值传给子弹,这样我就可以从玩家的位置闪电攻击子弹了?我可以不发送非静态值吗?随着我的移动,它会发生变化。在我的代码中,我有一个while循环总是在运行,所以它会更新项目符号位置和玩家位置。

再次感谢。

构造函数方式

最好的方法是将它通过Bullet的构造函数,如下所示:

Bullet::Bullet(float x, float y)
  : x_pos(x), y_pox(y)
{
  sprite = load_bitmap("bullet.bmp", 0);
}

使用示例:

Bullet bullet(player.getX(), player.getY()); // x_pos and y_pos are properly set

设置方式

如果你真的不能这样做,试着使用setters:

// Modifies x_pos from outside.
Bullet::setX(float x)
{
  this->x_pos = x;
}
// Modifies y_pos from outside.
Bullet::setY(float y)
{
  this->y_pos = y;
}

使用示例:

Bullet bullet;
bullet.setX(player.getX()); // x_pos is now properly set
bullet.setY(player.getY()); // y_pos is now properly set

@Ninetainedo提供了一个很好的答案,说明如何充分地从玩家到子弹头获取数据。

编辑:我在你的头文件中发现两个错误

第一:我看不到任何后卫在你的头上。也许您选择不复制/粘贴它们。

#ifndef PLAYER_H_
#define PLAYER_H_
class player
{
    // insert stuff here
};
#endif /* PLAYER_H_ */

第二:您的Bullet Constructor定义不应该是合格的

class Bullet
{
public:
    Bullet::Bullet();
};

应该是

class Bullet
{
public:
    Bullet();
};

在完成这两个修复之后,我能够用这个main编译您的代码,并获得以下结果。

player Mario;
Bullet BulletBill;
Mario.x_pos = 15.0;
Mario.y_pos = 25.0;
cout << "BulletBill (" << BulletBill.x_pos << "," << BulletBill.y_pos << ")" <<endl;
BulletBill.setX(Mario.getX());
BulletBill.setY(Mario.getY());
cout << "BulletBill (" << BulletBill.x_pos << "," << BulletBill.y_pos << ")" <<endl;

我想补充一点,也许你可以创建第三个对象,"Firearm",它弥合了玩家和子弹之间的差距。玩家"拥有一支"枪支,该枪支有一种工厂方法,可以使用枪支在建造师中的位置制造子弹。枪支的位置通过参考与所有者的位置相关联。

Bullet.h

Class Bullet
{
public:
    // -- Constructor
    Bullet (int x, int y) : x_pos(x), y_pos(y)
    {
        // Do other stuff here
    }
private:
    int x_pos;
    int y_pos;
};

Firearm.h

#include "bullet.h"
Class Firearm
{
public:
    // -- Constructor
    Firearm(const int& x, const int& y) : x_ref(x), y_ref(y)
    {
        // Do other stuff
    }
    // Create New Bullet Here
    Bullet Fire() const
    {
        return Bullet(x_ref, y_ref);
    }
private:
    const int& x_ref;
    const int& y_ref;   
};

Player.h

#include "bullet.h"
#include "firearm.h"
Class Player
{
public:
    Player(int x, int y) :  x_pos(x), y_pos(y)
    {
         // More constructor stuff here
    }
    void Update_Position()
    {
        // Update X_Pos and Y_Pos 
    }
    Bullet Pull_Trigger()
    {
        return gun.Fire();
    }
private:
    int x_pos;
    int y_pos;
    Firearm gun;
};
相关文章: