尝试在 C++ 中创建类似命令行游戏的东西

Trying to create something like command line game in C++

代码给我这些错误

Severity Code   Description Project File    Line    Suppression State
Error    C2664  'int printf(const char *const ,...)': cannot convert argument 2 from 'void' to '...'    game    C:\Dev\game\game\game\player.h  20  
Error    C2664  'Player::Player(char *,int *)': cannot convert argument 1 from 'int' to 'char *'    game    C:\Dev\game\game\game\main.cpp  8   

几个小时以来,我一直在尝试修复我的代码,但我想出了我应该做些什么来修复它。

entity.h:

#pragma once

#include <iostream>
#include <string>

class Entity {
public:
    int* hp;
    char* m_Name = nullptr;
    virtual char* GetName() { return m_Name; }
    virtual void Death(std::string message) {
        if (*hp <= 0) {
            std::cout << message << std::endl;
            *hp = 100;
        }
        else if (hp == nullptr)
            *hp = 100;
    }
};

player.h

#pragma once 

#include <iostream>
#include <string>
#include "entity.h"


class Player : public Entity {
public:
    char* m_Name;
    int* hp;
    Player(char* name, int* health)
        : m_Name(name), hp(health) {}; // m_name = name is the same
    char* GetName() override { return m_Name; }
    ~Player() { std::cout << "Why did you destroy me!" << std::endl; }
    int* getHealth() { return hp; }
    void Death() {
        if (*hp <= 0) {
            printf("%c was killed\n", *m_Name);
            *hp = 100;
        }
        else if (hp == nullptr)
            *hp = 100;
    }
};

main.cpp

#include <iostream>
#include "player.h"

int main() 
{
    Player* pp = new Player(135, "%");
}

玩家 class 第一个参数是 char* 第二个是 int* 所以:

int health = 100;
Player* pp = new Player("Alex", &health);

你的代码有很多问题。

  • 正如@AriaN 的回答中所指出的,您在 Player 构造函数中混淆了参数的顺序。

  • 你真的应该避免这些不必要的指示。特别是,您在 if (*hp <= 0) 中取消引用 hp。你再检查一下 nullptr 是不会执行的。

  • 你应该检查 if (hp != nullptr) 然后你可以安全地取消引用它,目前它的未定义行为因为你只在它是 nullptr 时取消引用它。

  • 你应该考虑给你的基础添加一个虚拟析构函数class。

  • 为什么 Entity::Death(...) 是虚拟的? Player::Death() 具有不同的签名并且不会覆盖它。

  • 您已经在基 class.

    中定义了 hpm_Name
  • 您正在泄漏内存,因为您从未在 main 中删除 pp

这是一个包含建议更改的示例:

#include <iostream>
#include <string>


class Entity {
public:
    int hp;
    std::string m_Name;
    Entity(const std::string &name, const int health) 
        : m_Name(name), hp(health){}
    virtual ~Entity() = default;
    virtual const std::string &GetName() const { return m_Name; }
    virtual void Death(const std::string &message="") {
        if (hp <= 0) {
            std::cout << message << std::endl;
            hp = 100;
        }
    }
};

class Player : public Entity {
public:
    Player(const std::string &name, const int health)
        : Entity(name, health) {}; // m_name = name is the same
    const std::string &GetName() const override { return m_Name; }
    ~Player() { std::cout << "Why did you destroy me!" << std::endl; }
    int getHealth() const { return hp; }
    void Death(const std::string &message="") override {
        if (hp <= 0) {
            std::cout << m_Name << " was killed.\n";
            std::cout << message;
            hp = 100;
        }
    }
};

int main() 
{
    int health = 135;
    Player* pp = new Player("Player", health);
    pp->hp = -1;
    pp->Death();
    delete pp;
}