没有合适的默认构造函数

时间:2018-11-22 16:02:19

标签: c++ error-handling sdl velocity

我收到一条错误消息:

  

SZ_GameItem-没有合适的默认构造函数。

以下是部分代码:

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

有帮助吗?

1 个答案:

答案 0 :(得分:2)

编写此行时:

SZ_GameItem exampleItem;

您实际上声明并初始化了SZ_GameItem类型的变量。在这种情况下,it implicitly initializes the variable using the default constructor(即不带任何参数),但是您没有提供。在某些情况下,可以隐式定义默认构造函数,但是当您提供了用户定义的构造函数时,其签名为SZ_GameItem(int x, int y, int w, int h)the default constructor is not implicitly defined

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