未解决的外部引用。

7

我有一个未解决的外部符号错误,让我非常苦恼。简而言之,我有一个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++ Express 2010
1个回答

11

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


3
谢谢,我将属性设置为“属性->C/C++->代码生成->运行时库”中的“Mt Debug DLL”,现在可以编译了。我不确定为什么会这样,但会进一步探究。 - Frank
2
我也曾经遇到过使用boost和opencv一起出现的问题。 可能与_DEBUG或NDEBUG标志有关,但在我的情况下,它们对此问题没有影响。 谢谢你们两个。 - Nuno Aniceto
1
将 _DEBUG 更改为 NDEBUG 解决了我们最近的头痛问题。谢谢! - Jon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接