带有默认析构函数的前向声明的unique_ptr不完整类型

时间:2019-06-01 11:45:19

标签: c++ unique-ptr incomplete-type

我的问题与此线程相关: std::unique_ptr with an incomplete type won't compile

我了解unique_ptr的不完整类型的概念,需要为保持类提供默认的析构函数。但是,我仍然收到以下错误:

 invalid use of incomplete type ‘class Game::Sprite’

以下是我的简化代码。请让我知道我是否还在这里吗? :

/*
 * Game.h
 */
#pragma once
#include <string>
#include <unordered_map>
#include "SDL.h"


class Game {
public:
    Game();
    ~Game();
    void run();

    const int SCREEN_WIDTH = 800;
    const int SCREEN_HEIGHT = 600;

private:
    void setup();
    void loadResources();
    void createWorld();
    void loop();

    window mWindow;
    renderer mRenderer;
    class Sprite;
    using sprite = std::unique_ptr<Sprite>;
    sprite mPlayer;

    struct Resource {
        std::string name;
        std::string path;
    };
    enum class ResourceType {
        TEXTURE, IMAGE, AUDIO
    };

    std::unordered_map<ResourceType, Resource> mResourceList;
    std::unordered_map<std::string, texture> mTextureHolder;
};

/*
 * Game.cpp
 */
#include <Game.h>
 ...

#include "Sprite.h"

Game::Game() {
    ...
}


void Game::run() {
    setup();
    loadResources();
    loop();
}

void Game::setup() {
    ...
}

void Game::loadResources() {
    ...
}

void Game::createWorld() {
    ...
}

void Game::loop() {
    bool stop = false;
    SDL_Event e;
    while (!stop) {
        while (SDL_PollEvent(&e) != 0) {
            if (e.type == SDL_QUIT) {
                stop = true;
            }
        }

        //clear screen
        SDL_RenderClear(mRenderer.get());

        //render texture
//      this gives error:
        mPlayer->renderFrame(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 0, 0);

        //update screen
        SDL_RenderPresent(mRenderer.get());
    }
}

//default destructor (tried explicit implmentation too)
Game::~Game() = default;

/*
 * Sprite.h
 */
#pragm once
#include "SDL.h"
#include <cstdint>

class Sprite {
public:
    Sprite(renderer renderer, texture texture, uint32_t width, uint32_t height);
    void renderFrame(uint32_t dstX, uint32_t dstY, uint32_t offsetX,
            uint32_t offsetY);
    void setTexture(texture texture);
private:
    renderer mRenderer;
    texture mTexture;
    uint32_t mWidth;
    uint32_t mHeight;
};

/*
 * Sprite.cpp
 */
#include "Sprite.h"

Sprite::Sprite(renderer renderer, texture texture, uint32_t width,
        uint32_t height) :
        mRenderer(renderer), mTexture(std::move(texture)), mWidth(width), mHeight(
                height) {
}

void Sprite::renderFrame(uint32_t dstX, uint32_t dstY, uint32_t offsetX,
        uint32_t offsetY) {
 ...
}

void Sprite::setTexture(texture texture) {
 ...
}

0 个答案:

没有答案