所以我是 C++ 新手,需要创建一个类。我认为这与 Sprite.cpp 中方法的实现有关。
谁能给我一个带有属性和方法的简单类的例子。或者至少让我知道我做错了什么?
错误 #1
Error   12  error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: void __thiscall std::_String_const_iterator<char,struct std::char_traits<char>,class std::allocator<char> >::_Compat(class std::_String_const_iterator<char,struct std::char_traits<char>,class std::allocator<char> > const &)const " (?_Compat@?$_String_const_iterator@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEXABV12@@Z)   D:\GSE\Game with Jon Bye\game\game\main.obj
错误 #2
Error   13  error LNK1120: 1 unresolved externals   D:\GSE\Game with Jon Bye\game\Debug\game.exe    1
精灵.h
#include <string>
#include <SDL.h>
#include "Functions.h"
using namespace std;
class Sprite
{
private:
    int PosX;
    int PosY;
    int xDist;
    int yDist;
    string ImagePath;
    SDL_Surface Screen;
    SDL_Surface *temp, *sprite, *screen;
public:
    Sprite(int PosX, int PosY, string ImagePath, SDL_Surface Screen );
    void Sprite::DrawSprite( int x, int y, SDL_Surface *sprite, SDL_Surface *screen )
    {
        //Make a temporary rectangle to hold the offsets
        SDL_Rect offset;
        //Give the offsets to the rectangle
        offset.x = x;
        offset.y = y;
        //Blit the surface
        SDL_BlitSurface( sprite, NULL, screen, &offset );
        SDL_UpdateRect(screen, 0, 0, 0, 0);
    }
    void Sprite::Draw()
    {
        #pragma region Char to String Conversion
        string ImagePath;
        char * writable = new char[ImagePath.size() + 1];
        copy(ImagePath.begin(), ImagePath.end(), writable);
        writable[ImagePath.size()] = '\0';
        #pragma endregion
        temp = SDL_LoadBMP(writable);
        sprite = SDL_DisplayFormat(temp);
        SDL_FreeSurface(temp);
        // free the string after using it
        delete[] writable;
        DrawSprite(PosX, PosY, sprite, screen);
    }
    void Sprite::Move(int xDist, int yDist)
    {
        PosX += xDist;
        PosY += yDist;
        Draw();
    };
};
精灵.cpp
#include "Sprite.h"
Sprite::Sprite(int posX, int posY, std::string imagePath, SDL_Surface screen) :        PosX(posX), PosY(posY), ImagePath(imagePath), Screen(screen)
{
    void DrawSprite( int x, int y, SDL_Surface *sprite, SDL_Surface *screen );
    void Draw();
    void Move(int xDist, int yDist);
}