Link error using std::vector

2019-09-10 02:22发布

问题:


I'm having a problem with a vector declaration.
Here's the code:

.h

#ifndef ANIMATEDSPRITE_H_
#define ANIMATEDSPRITE_H_

#include "Sprite.h"
#include <vector>

//using namespace std;

class AnimatedSprite //abstract class to point sprites
{
public:
    AnimatedSprite();
    ~AnimatedSprite();

    //gets and sets
    Sprite GetMySprite(int _index);
    void SetSpriteToList(Sprite _sprite);
    int GetState() const;
    void SetState(int _state);

    //other



private:
    std::vector<Sprite> spriteList;

    int state; //estado que esse sprite representa (parado esquerda, andando direita, etc)
};

#endif

.cpp

#include "AnimatedSprite.h"

AnimatedSprite::AnimatedSprite()
{
    spriteList.clear();
    state = NULL;
}

AnimatedSprite::~AnimatedSprite()
{

}

Sprite AnimatedSprite::GetMySprite(int _index)
{
    return spriteList[_index];
}

void AnimatedSprite::SetSpriteToList( Sprite _sprite )
{
    //Sprite* temp = new Sprite(1,2);
    spriteList.push_back(_sprite);
}

int AnimatedSprite::GetState() const
{
    return state;
}

void AnimatedSprite::SetState( int _state )
{
    state = _state;
}

But I'm getting 2 errors:

Error 1 error LNK2019: unresolved external symbol imp_CrtDbgReportW referenced in function "public: class Sprite & __thiscall std::vector >::operator[](unsigned int)" (??A?$vector@VSprite@@V?$allocator@VSprite@@@std@@@std@@QAEAAVSprite@@I@Z) AnimatedSprite.obj

Error 2 fatal error LNK1120: 1 unresolved externals C:\DevProjects\SDLSkeleton\Debug\SDLSkeleton.exe

I've found a solution removing the _DEBUG from the Preprocessor Definitions, but it seems kinda wrong to do that.
Is it the right solution? What's the consequence of removing it?
In the book and documentations I've checked it should be just a common variable declaration, but this errors showed up.

Thanks.

回答1:

This is because your build is inconsistent: you define _DEBUG macro, but link with release CRT version (/MD). So either remove _DEBUG, or select /MDd option.