将值从一个类传递到另一个类.单独的cpp文件.C++
Passing Values from one class to another. Separate cpp files . C++
我很难将值从一个CPP类文件传递到另一个CPP文件。
我一直在尝试大量不同的运算符来尝试传递值,我知道我遗漏了一些小东西,但我想不出其他什么可以做的,所以我在这里。
我正试图将player
类中的位置x_pos
和y_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;
};
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 运行同一解决方案的另一个项目的项目
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++从另一个类访问公共静态向量的正确方法是什么
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用std::transform将一个范围的元素添加到另一个范围中
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- C++试图读取一个文件并输出到另一个文本文件
- 如何将指针从一个void函数传递到另一个C++
- 如何从另一个文件继承私有成员变量和公共函数
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 如何在单独的线程上加载纹理到主内存并使用它在另一个线程上渲染
- 需要帮助将c字符串从嵌入式SQL提取复制到单独结构中的另一个c字符串
- 将值从一个类传递到另一个类.单独的cpp文件.C++
- 给定尺寸为 26 * 7 的图像是否有可能在垫子的每一行中包含 78 个单独的颜色值,而另一个可以包含 77
- 从一个文件和另一个单独的文件中取出时间,并将两个文件中的时间值放入c++列表中