在当前为对象的另一个类内部调用类函数
C calling a class function inside of another Class that is currently an Object
new here,所以要温柔,我目前正在为我的课程做我的主要项目,我不是要求为我做作业,我只是不能把我的头缠绕在一个奇怪的问题上,我有,没有能够找到答案,即使在这里。我正在使用SDL为我的绘图。
我正在做面向对象编程与我的项目或"状态机"(这听起来不那么痛苦在一个新手的头脑,相信我),并在我的类Game1.cpp的渲染部分,我试图调用我的玩家类的绘制函数,但由于一些未知的原因,我无法理解,它只是完全跳过这个函数调用。
我没有错误,我甚至使用断点来找出发生了什么,但它每次都完全跳过它,它也没有失败地绘制屏幕黑色。如果你能帮助我为什么跳过这个,我将非常感激。
老实说,我觉得这是一个简单的新手错误,但任何和所有的审查我的代码是受欢迎的,任何我能做的更好的自己是感激的。
Game1.cpp:
#include "Game1.h"
#include "PlayerCharacter.h"
Game1::Game1( World * worldObject )
{
//object setup
this->worldObject = worldObject;
setDone (false);
}
Game1::~Game1()
{
}
void Game1::handle_events()
{
//*******************************************
//**//////////////Call Input///////////////**
//*******************************************
//******Check for Keyboard Input*************
//******Check Keyboard Logic*****************
//******Check for Mouse Input****************
//The mouse offsets
x = 0, y = 0;
//If the mouse moved
if (SDL_PollEvent(&worldObject->event))
{
if( worldObject->event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = worldObject->event.motion.x;
y = worldObject->event.motion.y;
}
}
//******Check Mouse Logic********************
}
void Game1::logic()
{
//*******************************************
//**//////////Collision Detection//////////**
//*******************************************
//******Check Player Bullet Collision Loop***
//Check for collision with enemies
//Check for collision with bitmap mask (walls)
//******Check Enemy Bullet Collision Loop****
//Check for Collision with Player
//Check for collision with bitmap mask (walls)
}
void Game1::render()
{
//*******************************************
//**////////////////Drawing////////////////**
//*******************************************
//******Blit Black Background****************
SDL_FillRect(worldObject->Screen , NULL , 0xff000000);
//******Blit Bitmap Mask*********************
//******Blit Flashlight**********************
//******Blit Map*****************************
//******Blit Pickups*************************
//******Blit Bullets*************************
//******Blit Player**************************
&PlayerCharacter.Draw; // <----- Skips this line completely, no idea why
//******Blit Enemies*************************
//******Blit Blackened Overlay***************
//******Blit HUD*****************************
//******Flip Screen**************************
SDL_Flip(worldObject->Screen);
}
Game1.h
#ifndef __Game1_H_INLUDED__
#define __Game1_H_INLUDED__
#include "GameState.h"
#include "SDL.h"
#include "ImageLoader.h"
using namespace IMGLoader;
class Game1 : public GameState
{
private:
//Menu Image
World * worldObject;
SDL_Rect PauseMenu,Item1Tile,Item2Tile,Item3Tile;
/*bool bPauseMenu, bItem1Tile, bItem2Tile, bItem3Tile;
int ButtonSpace,ButtonSize;
float x,y;
int Alpha1,Alpha2;*/
//Clipping Window
//SDL_Rect sclip,dclip;
public:
//Loads Menu resources
Game1 (World * worldObject);
//Frees Menu resources
~Game1();
//Main loop functions
void handle_events();
void logic();
void render();
};
#endif
PlayerCharacter.cpp
#include "PlayerCharacter.h"
SDL_Rect psclip,pdclip;
PlayerCharacter::PlayerCharacter ( float X, float Y, float dX, float dY, float Angle, float Speed, bool Existance, int Height, int Width, int Health, int Shield, SDL_Surface* Player ):Characters ( X, Y, dX, dY, Angle, Speed, Existance, Height, Width, Health )
{
this->Player = Player;
this->Shield = Shield;
this->Player = load_image("imagePlayer1.png");
}
void PlayerCharacter::setShield ( int Shield )
{
this->Shield = Shield;
}
int PlayerCharacter::getShield ( void )
{
return Shield;
}
void PlayerCharacter::Draw( )
{
psclip.x = 0; psclip.y = 0; psclip.w = 64; psclip.h = 64;
pdclip.x = 640; pdclip.y = 318; pdclip.w = 64; pdclip.h = 64;
SDL_BlitSurface(Player, &psclip, worldObject->Screen, &pdclip);
}
PlayerCharacter.h
#ifndef __PlayerCharacter_H_INCLUDED__
#define __PlayerCharacter_H_INCLUDED__
#include "Characters.h"
class PlayerCharacter : public Characters
{
private:
int Shield;
SDL_Surface* Player;
World *worldObject;
public:
PlayerCharacter ( float X, float Y, float dX, float dY, float Angle, float Speed, bool Existance, int Height, int Width, int Health, int Shield, SDL_Surface* Player );
void setShield ( int Shield );
int getShield ( void );
void Draw ( );
};
#endif
&PlayerCharacter.Draw; // <----- Skips this line completely, no idea why
实际上不是一个函数调用。它是一个表达式,取PlayerCharacter
类中Draw
函数的地址,对它不做任何处理。
我真的有点惊讶,它编译没有错误,或者至少有大量的警告。
您需要创建一个PlayerCharacter
对象,然后调用该对象中的函数
&PlayerCharacter.Draw
不是函数调用。PlayerCharacter::Draw()
不是一个静态类方法,所以需要一个PlayerCharacter
对象来调用这个方法。
你有一个class PlayerCharacter
,它定义了PlayerCharacter
是什么以及可以做什么。但就我所见,你没有一个PlayerCharacter
对象,即没有玩家角色。如果你有一个,我们叫它pc
,然后你可以用pc.Draw()
来画它。为此,您必须实例化类,例如通过PlayerCharacter pc( ... )
,用...
替换为您在那里拥有的众多构造函数参数的一些适当值。(您确实需要一个默认构造函数,将所有这些初始化为零或其他适当的"开始"值…)
- join() 失败,如果在线程内部调用 io_context.run()
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 从动态加载的 dll 内部调用C++函数
- 如何在JNI中从线程内部调用JAVA方法
- 编译在 Python 代码内部调用的 C++ 代码时出错,使用 Boost Python
- 在VC++中从DLLMAIN内部调用D3D的CREATEDEVICE时,它会创建一个死锁(loaderlock?)。有没有办法克服这个问题?最终目标内
- 在成员函数内部调用成员函数
- WM_DESTROY没有在包裹的WndProc内部调用
- 为什么要在JNI内部调用纤维在JVM中抛出堆叠量
- 为什么逗号运算符在运算符[]中打电话给操作员[],而不是在Operator()内部调用
- 在程序集文件内部调用C++函数
- 从Python函数内部调用C++函数
- 使用 Rcpp 从内部调用 R 的 optim 函数C++
- C++ 错误 LNK1120:在静态函数内部调用时有 1 个未解析的外部
- 在应用程序内部调用linux命令行工具
- 当您在设备内部调用cudaMalloc时,实际会发生什么
- 通过传递外部指针或内部调用 new 构造的类,我可以在销毁时天真地删除这个指针吗?
- 在模板成员函数内部调用非模板成员函数
- 如何正确地从运算符构造函数内部调用复制构造函数
- NodeJS Addon从NAN AsyncWorker::Execute内部调用Javascript回调