C++ 将值从一个类传递到另一个类。单独的cpp文件。C++;
我正在努力将值从一个CPP类文件传递到一个单独的CPP文件 我一直在尝试加载不同的运算符来尝试传递值,我知道我遗漏了一些小的内容,但我想不出还有什么可以做的,所以我在这里 我正试图将C++ 将值从一个类传递到另一个类。单独的cpp文件。C++;,c++,allegro,C++,Allegro,我正在努力将值从一个CPP类文件传递到一个单独的CPP文件 我一直在尝试加载不同的运算符来尝试传递值,我知道我遗漏了一些小的内容,但我想不出还有什么可以做的,所以我在这里 我正试图将player类中的x_pos和y_pos位置传递到我的bullet类,以便子弹从播放器的位置发射。我只是无法将值传递给bullet类,其中包含标题 其中一些被注释掉了,现在我已经把它从同一个地方放了起来 #include "bullet.h" #include "player.h" Bullet::Bullet()
player
类中的x_pos
和y_pos
位置传递到我的bullet
类,以便子弹从播放器的位置发射。我只是无法将值传递给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;
}
我知道我做错了什么。我已经玩了一个小时了。收到这样的消息
错误非静态成员引用必须相对于特定对象
看别人的评论,我认为我没有把两个CPP文件连接起来,或者我没有从玩家坐标设置x位置
在游戏中,当我按下鼠标按钮时,它会从300400发射,并且只在这个位置发射
#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;
}
我想把玩家的x和y值传递给子弹,这样我就可以从玩家的位置上弹射子弹了?我可以不发送非静态值吗?当我四处走动时,一切都会改变。在我的代码中,我有一个while循环始终在运行,因此它将更新项目符号位置和播放器位置
#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
的构造函数:
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
Bullet bullet;
bullet.setX(player.getX()); // x_pos is now properly set
bullet.setY(player.getY()); // y_pos is now properly set
二传手方式
如果您确实不能这样做,请尝试使用:
使用示例:
Bullet bullet(player.getX(), player.getY()); // x_pos and y_pos are properly set
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构造函数定义不应该是合格的
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;
火器
#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;
};
不知道我是否理解正确。子弹知道有玩家吗?您是否有错误…?我建议将您的玩家类重命名为大写的玩家。一致的命名约定可以帮助其他类型的错误,如混淆类和实例。感谢回复,我已经玩了一个小时左右,似乎无法将值从player.cpp文件传递到bullet.cpp文件。错误非静态成员引用必须与特定对象相关?您能提供更多代码吗?显示您正在使用的头文件和主要示例?@Jerunh,您应该在
firemarm
类中添加更多的代码,因为它目前不会知道玩家的位置。@Ninetainedofiremarm
中的x和y位置是引用,所以这已经被处理。@AtlasC1哦,对不起,我没注意到=x