简体   繁体   English

错误:C++ 中的“此声明没有存储类或类型说明符”

[英]Error: "this declaration has no storage class or type specifier" in c++

I am trying to write some code that will make it easier for me to make SDL programs because I am learning and frequently start new projects.我正在尝试编写一些代码,使我更容易制作 SDL 程序,因为我正在学习并经常开始新项目。 It basically needs to do all the standard SDL stuff.它基本上需要完成所有标准的 SDL 工作。 This is the code这是代码

namespace engine {

    class Engine
    {
    private:
        SDL_Renderer* renderer;
        SDL_Window* window;
        SDL_Event event;

        bool initialised = true;
        bool quit = false;

    public:

        Engine(const char* name, int x, int y, int w = SDL_WINDOWPOS_CENTERED, int h = SDL_WINDOWPOS_CENTERED, bool fullscreen = false)
        {
            window = SDL_CreateWindow(name, x, y, w, h, fullscreen);
            if (window == nullptr)
                initialised = false;
            renderer = SDL_CreateRenderer(window, -1, 0);
            if (renderer == nullptr)
                initialised = false;
        }

        ~Engine()
        {
            SDL_DestroyWindow(window);
            SDL_DestroyRenderer(renderer);
        }

        void handleEvents()
        {
            while (SDL_PollEvent(&event))
            {
                if (event.type == SDL_QUIT)
                {
                    quit = true;
                }
            }
        }

        virtual void update()
        {

        }

        virtual void render()
        {

        }

        void start()
        {
            if (initialised && !quit)
            {
                handleEvents();
                update();
                render();
            }
        }
    };

}

And then inherit from this to make a game class然后从这个继承来制作一个游戏类

class Game : public engine::Engine
{
private:
    engine::Engine game{"engine", 1200, 600};

public:

    game.update() override
    {

    }

    game.render() override
    {

    }
};

And something like this in main主要是这样的

int main(int argc, char* argv[])
{
    Game game();
    game.start();
    return 0;
}

But I am getting the error specified in the title in line engine::Engine game{"engine", 1200, 600};但是我收到了标题中指定的错误engine::Engine game{"engine", 1200, 600}; in Game class.Game课上。 I don't know if what I am doing is possible or how to do it.我不知道我正在做的事情是否可行或如何去做。 Thanks for any help谢谢你的帮助

My Goal: I want some code, almost like a library that I can reuse with different SDL projects.我的目标:我想要一些代码,就像一个可以在不同 SDL 项目中重用的库。 This engine will be a separate library.这个引擎将是一个单独的库。 So I want some kind of engine class that inherits from engine in my project.所以我想要某种从我的项目中的引擎继承的引擎类。 Say the class that inherits form engine is game .说继承表单引擎的类是game I want game to override update and render, so that I can call different drawing methods defined in engine from render function.我希望游戏覆盖更新和渲染,以便我可以从渲染函数调用引擎中定义的不同绘图方法。 That way I wont have to make window, renderer and handle for events every time I start a project.这样我每次启动项目时都不必为事件制作窗口、渲染器和句柄。 I don't know if this is a good way to do it but any better solutions are welcome.我不知道这是否是一个好方法,但欢迎任何更好的解决方案。

I'm gutting Engine to make the example smaller.我正在删除Engine以使示例更小。 We don't need any of the SDL stuff in the class to show how to fix this up.我们不需要类中的任何 SDL 内容来展示如何解决这个问题。 Other than that, Engine looks pretty good.除此之外, Engine看起来还不错。 Don't need to change anything.不需要改变任何东西。

namespace engine {

    class Engine
    {
    private:

        bool initialised = true;
        bool quit = false;

    public:

        Engine(const char* , int , int ) // simplified to remove the SDL stuff
        {
        }

        ~Engine() // may want this to be virtual. See link about virtual destructors
        {
        }

        void handleEvents()
        {
        }

        virtual void update()
        {

        }

        virtual void render()
        {

        }

        void start()
        {
            if (initialised && !quit)
            {
                handleEvents();
                update();
                render();
            }
        }
    };

}

Where things start going wrong is in trying to implement Engine .事情开始出错的地方是尝试实现Engine I'm going to comment inline as I make changes.我将在进行更改时内联评论。

class Game : public engine::Engine // correct
{
    // engine::Engine game{"engine", 1200, 600};
    // don't need an engine::Engine as a member. Game IS an Engine. 
public:
    Game (): 
        engine::Engine {"engine", 1200, 600} // initialize base class here
    {

    }
    // game.update() override
    void update() override // use a normal function definition
                           // Compiler will figure out the rest 
    {

    }

    // game.render() override
    void render() override // same as above
    {

    }
};

There's one small bug in main . main有一个小错误。 The language has a little quirk where TYPE name() looks like a function, not a variable.该语言有一个小怪癖,其中TYPE name()看起来像一个函数,而不是一个变量。 This was cleaned up in C++11 with the addition of {} for more general initialization 1 .这在 C++11 中得到了清理,添加了{}以进行更一般的初始化1

int main()
{
    // Game game(); declares a function, game, that returns a Game.
    Game game; // defines a variable named game that is a Game. Could also be Game game{};
               // note the different braces.
    game.start();
    return 0;
}

1 Watch out when using this with container classes. 1将它与容器类一起使用时要小心。 vector<int> test{5}; is a vector with one element containing five, NOT five elements containing the default zero you get from vector<int> test(5);是一个vector ,其中一个元素包含五个,而不是包含从vector<int> test(5);获得的默认零的五个元素vector<int> test(5); . .

Some additional reading:一些补充阅读:

When to use virtual destructors? 何时使用虚拟析构函数?

C++, What does the colon after a constructor mean? C++,构造函数后面的冒号是什么意思?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM