未解析的外部

unresolved external

提问人:Frank 提问时间:8/12/2012 更新时间:8/12/2012 访问量:4504

问:

我有一个未解决的外部符号错误,这让我发疯。简而言之,我有一个用于SDL_Surfaces的包装类 ('DgSurface') 和一个用于加载和存储 DgSurfaces 的类 ('DgSurfaceList')。尝试在我的项目中包含 DgSurfaceList 文件时出现链接问题。以下是我的课程:

头文件“DgSurface.h”包含 DgSurface 类声明:

    #ifndef DGSURFACE_H
    #define DGSURFACE_H

    #include "SDL.h"
    #include <string>

    class DgSurface
    {
    public:

        //Constructor/destructor
        DgSurface(std::string N, SDL_Surface* I): image(I), name(N) {}
        DgSurface() {name = ""; image = NULL;}
        ~DgSurface();

        //Copy operations
        DgSurface(const DgSurface&);
        DgSurface& operator= (const DgSurface&);

        //Data members
        std::string name;       //The name of the image
        SDL_Surface* image;     //The image
    };

    #endif

cpp 文件“DgSurface.cpp”包含 DgSurface 定义:

#include "DgSurface.h"
#include "SDL.h"

//--------------------------------------------------------------------------------
//        Constructor
//--------------------------------------------------------------------------------
DgSurface::DgSurface(const DgSurface& other)
{
    //Copy name
    name = other.name;

    //Create new SDL_Surface 
    image = SDL_ConvertSurface(other.image, other.image->format, 0);
}


//--------------------------------------------------------------------------------
//        Destructor
//--------------------------------------------------------------------------------
DgSurface::~DgSurface()
{
    SDL_FreeSurface(image);
}


//--------------------------------------------------------------------------------
//        Assignment operator
//--------------------------------------------------------------------------------
DgSurface& DgSurface::operator= (const DgSurface& other)
{
    // if same object
    if ( this == &other )
        return *this;

    //Copy name
    name = other.name;

    //Create new SDL_Surface 
    image = SDL_ConvertSurface(other.image, other.image->format, 0);

    return *this;
}

这个类似乎工作正常,并按预期执行(但是,一如既往,我对反馈:)持开放态度。

“DgSurfaceList.h”包含 DgSurfaceList 类声明:

#ifndef DGSURFACELIST_H
#define DGSURFACELIST_H

#include "SDL.h"
#include <list>
#include <string>
#include "DgSurface.h"


class DgSurfaceList
{
    public:
        //Constructors/destructor
        DgSurfaceList() {}
        ~DgSurfaceList() {}

        //Functions
        bool AddImage(std::string location, std::string name);

        //Return Functions
        SDL_Surface* GetImage(std::string S) const;

    private:
        //Data members
        std::list<DgSurface> imlist;    //The list of DgSurfaces

        //Functions
        SDL_Surface* LoadImage( std::string filename );
};


#endif

最后,“DgSurfaceList.cpp”包含DgSurfaceList定义:

#include "SDL.h"
#include "SDL_image.h"
#include <list>
#include <string>
#include "DgSurface.h"
#include "DgSurfaceList.h"


//--------------------------------------------------------------------------------
//      Load an image from file
//--------------------------------------------------------------------------------
SDL_Surface* DgSurfaceList::LoadImage( std::string filename )
{
    //Loads an image from file, returns SDL_surface*
    ...

}   //End:DgSurfaceList::LoadImage()


//--------------------------------------------------------------------------------
//      Add a DgSurface to the list
//--------------------------------------------------------------------------------
bool DgSurfaceList::AddImage(std::string location, std::string name) 
{
    //Load the image
    DgSurface temp(name,LoadImage(location));

    //If there was an error in loading the image
    if( temp.image == NULL )
        return false;

    //If everything loaded fine, place a copy into imlist
    imlist.push_back(temp);

    return true;

}   //End: DgSurfaceList::AddImage();


//--------------------------------------------------------------------------------
//      Searches imlist for an image, returns a pointer to a SDL_Surface
//--------------------------------------------------------------------------------
SDL_Surface* DgSurfaceList::GetImage(std::string S) const
{
    std::list<DgSurface>::const_iterator i;

    //Search imlist for DgSurface of the same name
    for (i = imlist.begin(); i != imlist.end(); i++)
    {
        if (S.compare((*i).name) == 0)
            return (*i).image;
    }

    //Return Null if name not found
    return NULL;

}   //End:DgSurfaceList::GetImage()

现在,如果我注释掉 cpp 文件中的 DgSurfaceList::GetImage() 定义,DgSurfaceList 似乎可以正常工作并正确存储图像。更具体地说,只有当我在上述函数中包含 for 循环时才会出现链接错误。它可能是什么?

其他信息:

错误:

unresolved external symbol __imp___CrtDbgReportW referenced in function "public: class 
DgSurface const & __thiscall std::_List_const_iterator<class std::_List_val<class 
DgSurface,class std::allocator<class DgSurface> > >::operator*(void)const "

编码环境: Visual C++ 速成版 2010

C++ 链接器 SDL 符号未 解析的外部

评论


答:

11赞 jahhaj 8/12/2012 #1

CrtDbgReport 仅在 C 运行时库的调试版本中定义。因此,也许您正在调试模式下编译,但与库的发布版本链接。另一种可能性是,您正在以发布模式进行编译,但您定义的某些宏导致编译 std::list 的调试版本。

评论

3赞 Frank 8/12/2012
谢谢,我将 Properties->C/C++->Code Generation->Runtime Library 设置为 Mt Debug DLL,现在可以编译了。我不确定为什么会这样,但会调查一下。
2赞 Nuno Aniceto 6/2/2015
我同时使用 boost 和 opencv 也遇到了这个问题。有与 _DEBUG 或 NDEBUG 标志相关的可能解释,就我而言,它们对此问题没有影响。谢谢你们俩。
1赞 Jon 6/23/2016
将_DEBUG更改为 NDEBUG 解决了我们最近令人头疼的问题。谢谢!