简体   繁体   中英

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. It basically needs to do all the standard SDL stuff. 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}; in Game class. 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. 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 . 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. We don't need any of the SDL stuff in the class to show how to fix this up. Other than that, Engine looks pretty good. 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 . 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 . The language has a little quirk where TYPE name() looks like a function, not a variable. This was cleaned up in C++11 with the addition of {} for more general initialization 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. 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); .

Some additional reading:

When to use virtual destructors?

C++, What does the colon after a constructor mean?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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