Probleme mit Pointern

  • C++

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von Gonger96.

    Probleme mit Pointern

    Servus, hab grad das Problem, dass mir en Fehler bei dem Code geschmissen wird den ich nicht nachvollziehen kann.

    C-Quellcode

    1. #ifndef PLAYER_INPUT_COMPONENT_H_
    2. #define PLAYER_INPUT_COMPONENT_H_
    3. #include <SFML/Graphics.hpp>
    4. #include <memory>
    5. #include "InputComponent.h"
    6. #include "../Commands/PlayerJumpCommand.h"
    7. namespace t1
    8. {
    9. //forward declaration
    10. class Player;
    11. using Key = sf::Keyboard::Key;
    12. //Component class for handling Player input
    13. class PlayerInputComponent : public InputComponent<Player>
    14. {
    15. public:
    16. PlayerInputComponent() {};
    17. std::unique_ptr<Command<Player>> handleInput() override;
    18. private:
    19. };
    20. std::unique_ptr<Command<Player>> PlayerInputComponent::handleInput()
    21. {
    22. if(sf::Keyboard::isKeyPressed(Key::W))
    23. return std::unique_ptr<PlayerJumpCommand> (new PlayerJumpCommand{});
    24. else if(sf::Keyboard::isKeyPressed(Key::A))
    25. return nullptr;
    26. else if(sf::Keyboard::isKeyPressed(Key::D))
    27. return nullptr;
    28. else
    29. return nullptr;
    30. }
    31. }
    32. #endif


    Der Vollständigkeit halber hier noch die wichtigen anderen Klassen:

    C-Quellcode

    1. #ifndef COMMAND_H_
    2. #define COMMAND_H_
    3. namespace t1
    4. {
    5. template<class Actor>
    6. class Command
    7. {
    8. public:
    9. virtual ~Command() {};
    10. virtual void execute(Actor &actor) = 0;
    11. };
    12. }
    13. #endif


    C-Quellcode

    1. #ifndef PLAYER_JUMP_COMMAND_H_
    2. #define PLAYER_JUMP_COMMAND_H_
    3. #include "Command.h"
    4. #include "../Player.h"
    5. namespace t1
    6. {
    7. class PlayerJumpCommand : Command<Player>
    8. {
    9. public:
    10. void execute(Player &player);
    11. };
    12. void PlayerJumpCommand::execute(Player &player)
    13. {
    14. player.jump();
    15. }
    16. }
    17. #endif


    C-Quellcode

    1. #ifndef INPUT_COMPONENT_H_
    2. #define INPUT_COMPONENT_H_
    3. #include "../Commands/Command.h"
    4. #include <memory>
    5. namespace t1
    6. {
    7. template<class T>
    8. class InputComponent
    9. {
    10. public:
    11. virtual ~InputComponent();
    12. virtual std::unique_ptr<Command<T>> handleInput();
    13. };
    14. }
    15. #endif


    Bei dem Code bekomm ich folgenden Fehler und kann mir aber nicht erklären wieso. Blickt einer von euch C++ Gurus durch was da schief läuft ?

    Quellcode

    1. include/Components/PlayerInputComponent.h: In member function ‘virtual std::unique_ptr<t1::Command<t1::Player>, std::default_delete<t1::Command<t1::Player> > > t1::PlayerInputComponent::handleInput()’:
    2. include/Components/PlayerInputComponent.h:30:70: error: could not convert ‘std::unique_ptr<t1::PlayerJumpCommand>((operator new(8ul), (<statement>, ((t1::PlayerJumpCommand*)<anonymous>))))’ from ‘std::unique_ptr<t1::PlayerJumpCommand>’ to ‘std::unique_ptr<t1::Command<t1::Player>, std::default_delete<t1::Command<t1::Player> > >’
    3. return std::unique_ptr<PlayerJumpCommand> (new PlayerJumpCommand{});
    4. ^
    5. make: *** [debug] Fehler 1
    Hatte ich auch schon versucht, kommt dann Folgender Fehler bei raus:
    Passt hab bei PlayerJumpCommand die Vererbung nicht public gemacht. Jetzt passts, danke.
    Jo das funktioniert jetzt. Noch ne Frage wie ist das eigentlich Performance Technisch. Schlägt das arg auf die Performance jedes mal Speicher neu zu allozieren ? Der unique_ptr hat ja soweit ich weiß nicht viel overhead, könnte aber bei vielen Iterationen doch en problem werden oder ?
    new ist immer sehr teuer. Schlägt dementsprechend auch stark auf die Performance. Ich würde garnicht von Command<> erben, sondern eine Methode im Template zur Verfügung stellen und Command<> einfach übern Stack zurückgeben, oder als RValue etc.