在当前为对象的另一个类内部调用类函数

C calling a class function inside of another Class that is currently an Object

本文关键字:内部 调用 类函数 另一个 对象      更新时间:2023-10-16

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( ... ),用...替换为您在那里拥有的众多构造函数参数的一些适当值。(您确实需要一个默认构造函数,将所有这些初始化为零或其他适当的"开始"值…)