无法解析的外部

问题描述:

我有一个无法解析的外部符号错误,导致我疯了。总之,我有一个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” contatins 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() 

现在,如果我注释掉DgSurfaceList ::的getImage ()定义在cpp文件中,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

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

+3

谢谢,我设置属性 - > C/C++ - >代码生成 - >运行时库到Mt Debug DLL,现在可以编译。我不确定这是为什么,但会研究它。 – Frank 2012-08-12 06:10:20

+2

我也有这个问题一起使用boost和opencv。 有可能的解释与_DEBUG或NDEBUG标志相关,在我的情况下他们没有影响这个问题。 谢谢你们两位。 – 2015-06-02 11:06:03

+1

将_DEBUG更改为NDEBUG解决了我们最近的头痛。谢谢! – Jon 2016-06-22 19:48:32