Search code examples
c++error-handlingsdlvelocity

No appropriate default constructor available


I'm getting an error that says:

SZ_GameItem - No appropriate default constructor available.

Here's parts of the codes:

main.cpp:

#include <iostream>

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"

using namespace std;

#include "SZ_timer.h"
SZ_Timer aTimer;
const int DELTA_TIME = 10;
bool done = false;

#include "SZ_Player.h"
SZ_Player examplePlayer;

#include "SZ_GameItem.h"
SZ_GameItem exampleItem;

int main(int argc, char *argv[])
{
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0);

    // Creates the game window
    SDL_Window* game_window = SDL_CreateWindow("Rise", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

    // Creates the game render to draw on the game window.
    SDL_Renderer* game_renderer = SDL_CreateRenderer(game_window, 0, 0);

    // Game Loop
    examplePlayer.Init();
    while (!done)
    {
        aTimer.resetTicksTimer();

        examplePlayer.Update();
        examplePlayer.Input();

        exampleItem.Update();

        SDL_SetRenderDrawColor(game_renderer, 0, 0, 20, SDL_ALPHA_OPAQUE);
        SDL_RenderClear(game_renderer);
        examplePlayer.Render(game_renderer);
        exampleItem.Render(game_renderer);
        SDL_RenderPresent(game_renderer);

        // If less time has passed than allocated block, wait difference
        if (aTimer.getTicks() < DELTA_TIME)
        {
            SDL_Delay(DELTA_TIME - aTimer.getTicks());
        }
    }
    SDL_Quit();

    // Exits program
    return 0;
}

SZ_GameItem.cpp:

#include "SZ_GameItem.h"

SZ_GameItem::SZ_GameItem(int eX, int eY, int eW, int eH)
{
    x = eX;
    y = eY;
    height = eH;
    width = eW;

    rect.x = x;
    rect.y = y;
    rect.h = height;
    rect.w = width;

    velocity.x = 1;
    velocity.y = 0;
}

SZ_GameItem::~SZ_GameItem()
{

}

void SZ_GameItem::Input()
{

}

void SZ_GameItem::Update()
{
    rect.x = x;
    rect.y = y;
    rect.h = height;
    rect.w = width;

    x = x + velocity.x;
    y = y + velocity.y;
}

void SZ_GameItem::Render(SDL_Renderer* pRenderer)
{
    SDL_SetRenderDrawColor(pRenderer, 255, 255, 255, 255);
    SDL_RenderDrawRect(pRenderer, &rect);
}

SZ_GameItem.h:

#ifndef aGameItem
#define aGameItem

#include <iostream>
#include "SDL.h"
#include "SZ_Vector2D.h"

class SZ_GameItem
{
public:
    SZ_GameItem(int x, int y, int w, int h);
    ~SZ_GameItem();

    void Update();
    void Input();
    void Render(SDL_Renderer* aRenderer);

    SZ_Vector2D velocity;
    int x, y, height, width;

private:
    SDL_Rect rect;
};

#endif

Solution

  • When you write this line:

    SZ_GameItem exampleItem;
    

    you actually declare and initialize a variable of type SZ_GameItem. And in this case, it implicitly initializes the variable using the default constructor (that is, which takes no argument), but you haven't provided one. A default constructor can be implicitly defined in some cases, but as you provided a user-defined constructor, with signature SZ_GameItem(int x, int y, int w, int h), the default constructor is not implicitly defined.

    What needs be done is to either initialize this variable yourself using the constructor your provided, or provide a default constructor.