没有合适的默认构造函数可用

No appropriate default constructor available

我收到一条错误消息:

SZ_GameItem - No appropriate default constructor available.

部分代码如下:

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

当你写这行时:

SZ_GameItem exampleItem;

您实际上声明并初始化了一个 SZ_GameItem 类型的变量。在这种情况下,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.

需要做的是使用您提供的构造函数自己初始化此变量,或者提供默认构造函数。