【问题标题】:Link error using std::vector使用 std::vector 链接错误
【发布时间】:2012-05-01 16:50:26
【问题描述】:


我在使用矢量声明时遇到问题。
这是代码:

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

但我收到 2 个错误:

错误 1 ​​错误 LNK2019:未解析的外部符号 imp_CrtDbgReportW 在函数“public: class Sprite & __thiscall std::vector >::operator[](unsigned int)”(??A? $vector@VSprite@@V?$allocator@VSprite@@@std@@@std@@QAEAAVSprite@@I@Z) AnimatedSprite.obj

错误 2 致命错误 LNK1120: 1 未解决的外部 C:\DevProjects\SDLSkeleton\Debug\SDLSkeleton.exe

我找到了从预处理器定义中删除 _DEBUG 的解决方案,但这样做似乎有点错误。
这是正确的解决方案吗?去掉有什么后果?
在我检查过的书和文档中,它应该只是一个公共变量声明,但出现了这个错误。

谢谢。

【问题讨论】:

    标签: c++ vector hyperlink std


    【解决方案1】:

    这是因为您的构建不一致:您定义了 _DEBUG 宏,但与发布 CRT 版本 (/MD) 链接。所以要么删除 _DEBUG,要么选择 /MDd 选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      相关资源
      最近更新 更多