简体   繁体   English

未解决的外部

[英]unresolved external

I have an unresolved external symbol error that's driving me nuts. 我有一个无法解决的外部符号错误,这使我发疯。 In short, I have a wrapper class for SDL_Surfaces ('DgSurface') and a class to load and store DgSurfaces ('DgSurfaceList'). 简而言之,我有一个SDL_Surfaces包装类(“ DgSurface”)和一个用于加载和存储DgSurfaces的类(“ DgSurfaceList”)。 The link issue arises when trying to include the DgSurfaceList files in my project. 尝试在我的项目中包含DgSurfaceList文件时出现链接问题。 Here are my classes: 这是我的课程:

The header file "DgSurface.h" contains the DgSurface class declaration: 头文件“ 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

The cpp file "DgSurface.cpp" contatins DgSurface definitions: 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;
}

This class seems to work fine and performs as expected (however, as always, am open to feedback :). 该课程似乎运行良好,并且按预期方式运行(但是,一如既往,我们随时可以提供反馈意见:)。

"DgSurfaceList.h" contains DgSurfaceList class declarations: “ 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

and finally "DgSurfaceList.cpp" contains DgSurfaceList definitions: 最后,“ 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()

Now, if I comment out the DgSurfaceList::GetImage() definition in the cpp file, DgSurfaceList seems to work fine and store images correctly. 现在,如果我注释掉cpp文件中的DgSurfaceList :: GetImage()定义,则DgSurfaceList似乎可以正常工作并正确存储图像。 More specifically, the link error only arises when I include the for loop in the above function. 更具体地说,仅当我在上述函数中包含for循环时,才会出现链接错误。 What could it be? 会是什么呢?

Other info: 其他资讯:

Error: 错误:

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 "

Coding environment: Visual C++ express 2010 编码环境:Visual C ++ Express 2010

CrtDbgReport is defined in the debug version of the C run time library only. CrtDbgReport仅在C运行时库的调试版本中定义。 So perhaps you are compiling in debug mode but linking with the release version of the library. 因此,也许您是在调试模式下进行编译,但与库的发行版链接。 Another possibility is that you are compiling in release mode but that some macro you have defined is causing the debug version of std::list to be compiled. 另一种可能性是您正在发布模式下进行编译,但是您定义的某些宏正在导致编译std :: list的调试版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM