LNK2019 C ++类定义中的错误需要示例

时间:2012-12-16 01:41:42

标签: c++ class header

  

可能重复:
  How do you get a minimal SDL program to compile and link in visual studio 2008 express?

所以我是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

Sprite.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();
    };
};

Sprite.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);
}

2 个答案:

答案 0 :(得分:0)

啊,我的伙伴Jonathan O!看起来你接受了我的建议毕竟使用SDL ......

我不确定你的错误意味着什么,但我知道在SDL中,你需要把它放在:

#undef main

在你之后:

#include <SDL.h> 

否则它将无效。为什么?由于某种原因,SDL在某处定义了“main”,因此当你包含“SDL.h”时,链接器就会变得怪异,并且会让所有人感到困惑(因为main是入口函数)。

也许这是您的错误的来源,虽然我对此表示怀疑,但错误消息看起来与字符串有关......

另外,我真的不知道你的Sprite.CPP文件中发生了什么。如果您还是C ++的新手并且正在研究如何使用C ++创建类,那么您将在这里找到一些C ++教程,这就是我开始学习C ++的方式:http://thenewboston.org/list.php?cat=16

答案 1 :(得分:0)

Unresolved externals in C++ when using vectors and find

我处于调试模式而不是发布模式。

确保在所选的新版本模式中设置链接器设置等,并且您正在构建发布项目。