vc ++ MFC应用程序,错误LNK2019:未解析的外部符号

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vc ++ MFC应用程序,错误LNK2019:未解析的外部符号相关的知识,希望对你有一定的参考价值。

在Visual Studio vc ++中编译这个游戏示例时,我有一个神秘的(至少对我来说)错误LNK2019:未解析的外部符号。

完整错误:

错误错误LNK2019:未解析的外部符号“public:__thiscall Figure :: Figure(void)”(?? 0Figure @@ QAE @ XZ)在函数“protected:__thiscall CTetrisDoc :: CTetrisDoc(void)”中引用(?? 0CTetrisDoc @@ IAE @ XZ)C: Users ary Documents Visual Studio 2013 Projects Tetris Tetris TetrisDoc.obj Tetris

图标题:

#ifndef FIGURE_H
#define FIGURE_H
#include "Square.h"

const COLORREF BLACK = RGB(0, 0, 0);
const COLORREF WHITE = RGB(255, 255, 55);
const COLORREF DEFAULT_COLOR = WHITE;
class ColorGrid;
extern int g_iRowHeight, g_iColWidth;
enum  { NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3 };
const int SQUARE_ARRAY_SIZE = 4;
const int SQUARE_INFO_SIZE = 4;
typedef Square SquareArray[SQUARE_ARRAY_SIZE];
typedef SquareArray SquareInfo[SQUARE_INFO_SIZE];
class Figure
{
public:
    Figure(); 
    Figure(int iDirection, COLORREF rfColor, const SquareInfo& squareInfo);
    Figure operator=(const Figure& figure);
    void SetColorGrid(ColorGrid* pColorGrid) { m_pColorGrid = pColorGrid; }
public:
    BOOL IsFigureValid() const; 
    BOOL MoveLeft(); 
    BOOL MOveRight();
private:
    void RotateClockwiseOneQuarter(); 
    void RotateCounterclockwiseOneQuarter(); 
public:
    BOOL RotateClockwise(); 
    BOOL RotateCounterClockwise();
    BOOL MoveDown(); 
    void AddToGrid(); 
    CRect GetArea() const; 
public:
    void Draw(int iColorStatus, CDC* pDC) const; 
    friend void DrawSquare(int iRow, int iCol, CDC* pDC);
public:
    void Serialize(CArchive& archive);
private:
    COLORREF m_rfColor; 
    ColorGrid* m_pColorGrid; 
    int m_iRow, m_iCol, m_iDirection; 
    SquareInfo m_squareInfo; 

};

#endif

TetrisDoc.h header file:


// TetrisDoc.h : interface of the CTetrisDoc class
//

#pragma once
#include "Figure.h"
#include "ColorGrid.h"

typedef CList<int> IntList; 
const int FIGURE_ARRAY_SIZE = 7; 
class CTetrisDoc : public CDocument
{
protected: // create from serialization only
    CTetrisDoc();
    DECLARE_DYNCREATE(CTetrisDoc)
public:
    virtual ~CTetrisDoc();


private:
    ColorGrid m_colorGrid; 
    Figure m_activeFigure, m_nextFigure;
    int m_iScore;
    IntList m_scoreList;
    const CRect NEXT_AREA, SCORE_AREA;
    static Figure m_figureArray[FIGURE_ARRAY_SIZE];

// Operations
public:
    void SaveScoreList();
    virtual void Serialize(CArchive& archive);
    int GetScore() const { return m_iScore; }
    const IntList* GetScoreList() { return &m_scoreList; }
    const ColorGrid* GetGrid() 
    { 

        return &m_colorGrid;
    }
    const Figure& GetActiveFigure() { return m_activeFigure; }
    const Figure& GetNextFigure() { return m_nextFigure; }
    void LeftArrowKey(); 
    void RightArroyKey(); 
    void UpArrowKey(); 
    void DownArrowKey();
    BOOL Timer(); 
    void SpaceKey(); 
    enum COLOR {RED=0,BROWN=1,TURQUOISE=2,GREEN=3,BLUE=4,PURPLE=5,YELLOW=6};
    enum ORIENTATION { NORTH = 0, SOUTH = 1,  EAST = 2, WEST = 3 };


// Overrides
public:
    virtual BOOL OnNewDocument();
#ifdef SHARED_HANDLERS
    virtual void InitializeSearchContent();
    virtual void OnDrawThumbnail(CDC& dc, LPRECT lprcBounds);
#endif // SHARED_HANDLERS

#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif
// Generated message map functions
protected:
    DECLARE_MESSAGE_MAP();

private:
    void GameOver(); 
    BOOL NewGame(); 
    int AddScoreToList(); 
    void DeleteFullRows(); 
    BOOL IsRowFull(int iRow);
    void FlashRow(int iFlashRow);
    void DeleteRow(int iDeleteRow);

#ifdef SHARED_HANDLERS
    // Helper function that sets search content for a Search Handler
    void SetSearchContent(const CString& value);
#endif // SHARED_HANDLERS
};

TetrisDoc.h文件:

// TetrisDoc.h : interface of the CTetrisDoc class
//

#pragma once
#include "Figure.h"
#include "ColorGrid.h"

typedef CList<int> IntList; 
const int FIGURE_ARRAY_SIZE = 7; 
class CTetrisDoc : public CDocument
{
protected: // create from serialization only
    CTetrisDoc();
    DECLARE_DYNCREATE(CTetrisDoc)
public:
    virtual ~CTetrisDoc();


private:
    ColorGrid m_colorGrid; 
    Figure m_activeFigure, m_nextFigure;
    int m_iScore;
    IntList m_scoreList;
    const CRect NEXT_AREA, SCORE_AREA;
    static Figure m_figureArray[FIGURE_ARRAY_SIZE];

// Operations
public:
    void SaveScoreList();
    virtual void Serialize(CArchive& archive);
    int GetScore() const { return m_iScore; }
    const IntList* GetScoreList() { return &m_scoreList; }
    const ColorGrid* GetGrid() 
    { 

        return &m_colorGrid;
    }
    const Figure& GetActiveFigure() { return m_activeFigure; }
    const Figure& GetNextFigure() { return m_nextFigure; }
    void LeftArrowKey(); 
    void RightArroyKey(); 
    void UpArrowKey(); 
    void DownArrowKey();
    BOOL Timer(); 
    void SpaceKey(); 
    enum COLOR {RED=0,BROWN=1,TURQUOISE=2,GREEN=3,BLUE=4,PURPLE=5,YELLOW=6};
    enum ORIENTATION { NORTH = 0, SOUTH = 1,  EAST = 2, WEST = 3 };


// Overrides
public:
    virtual BOOL OnNewDocument();
#ifdef SHARED_HANDLERS
    virtual void InitializeSearchContent();
    virtual void OnDrawThumbnail(CDC& dc, LPRECT lprcBounds);
#endif // SHARED_HANDLERS

#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif
// Generated message map functions
protected:
    DECLARE_MESSAGE_MAP();

private:
    void GameOver(); 
    BOOL NewGame(); 
    int AddScoreToList(); 
    void DeleteFullRows(); 
    BOOL IsRowFull(int iRow);
    void FlashRow(int iFlashRow);
    void DeleteRow(int iDeleteRow);

#ifdef SHARED_HANDLERS
    // Helper function that sets search content for a Search Handler
    void SetSearchContent(const CString& value);
#endif // SHARED_HANDLERS
};

TetrisDoc.cpp文件:

// TetrisDoc.cpp : implementation of the CTetrisDoc class
//

#include "stdafx.h"

// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "Tetris.h"
#endif

#include "TetrisDoc.h"

#include <propkey.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

/*CTetrisDoc::ORIENTATION NORTH = NORTH; 
CTetrisDoc::ORIENTATION EAST = EAST;
CTetrisDoc::ORIENTATION WEST = WEST;
CTetrisDoc::ORIENTATION SOUTH = SOUTH;*/

/*Figure redFigure(NORTH, RED, RedInfo);
Figure brownFigure(EAST, BROWN, BrownInfo);
Figure turquoiseFigure(EAST, TURQUOISE.TurquoiseInfo);
Figure greenFigure(EAST, GREEN, GreenInfo);
Figure blueFigure(SOUTH, BLUE, BlueInfo);
Figure purpleFigure(SOUTH, PURPLE, PurpleInfo);
Figure yellowFigure(SOUTH, YELLOW, YellowInfo);
Figure CTetrisDoc::m_figureArray[] = { redFigure, brownFigure, turquoiseFigure, greenFigure, yellowFigure, blueFigure, purpleFigure };*/

// CTetrisDoc

IMPLEMENT_DYNCREATE(CTetrisDoc, CDocument)
BEGIN_MESSAGE_MAP(CTetrisDoc, CDocument)
END_MESSAGE_MAP()

/*CTetrisDoc::LeftArrowKey()
{
    CRect rcOldArea = m_activeFigure.GetArea(); 
    if (m_activeFigure.MoveLeft())
    {
        CRect rcNewArea = m_activeFigure.GetArea(); 
        UpdateAllViews(NULL, COLOR, (CObject*)&rcOldArea);
        UpdateAllViews(NULL, COLOR, (CObject*)&rcNewArea);
        SetModifiedFlag(); 
    }


}*/
// CTetrisDoc construction/destruction

CTetrisDoc::CTetrisDoc()
{
    // TODO: add one-time construction code here

}

CTetrisDoc::~CTetrisDoc()
{
}

BOOL CTetrisDoc::OnNewDocument()
{
    if (!CDocument::OnNewDocument())
    {
        return FALSE;
    }
    else
    {
        return TRUE;
    }
    // TODO: add reinitialization code here
    // (SDI documents will reuse this document)

}

// CTetrisDoc serialization

void CTetrisDoc::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
        // TODO: add storing code here
    }
    else
    {
        // TODO: add loading code here
    }
}

#ifdef SHARED_HANDLERS

// Support for thumbnails
void CTetrisDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
    // Modify this code to draw the document's data
    dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

    CString strText = _T("TODO: implement thumbnail drawing here");
    LOGFONT lf;

    CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
    pDefaultGUIFont->GetLogFont(&lf);
    lf.lfHeight = 36;

    CFont fontDraw;
    fontDraw.CreateFontIndirect(&lf);

    CFont* pOldFont = dc.SelectObject(&fontDraw);
    dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
    dc.SelectObject(pOldFont);
}

// Support for Search Handlers
void CTetrisDoc::InitializeSearchContent()
{
    CString strSearchContent;
    // Set search contents from document's data. 
    // The content parts should be separated by ";"

    // For example:  strSearchContent = _T("point;rectangle;circle;ole object;");
    SetSearchContent(strSearchContent);
}

void CTetrisDoc::SetSearchContent(const CString& value)
{
    if (value.IsEmpty())
    {
        RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
    }
    else
    {
        CMFCFilterChunkValueImpl *pChunk = NULL;
        ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
        if (pChunk != NULL)
        {
            pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
            SetChunkValue(pChunk);
        }
    }
}

#endif // SHARED_HANDLERS

// CTetrisDoc diagnostics

#ifdef _DEBUG
void CTetrisDoc::AssertValid() const
{
    CDocument::AssertValid();
}

void CTetrisDoc::Dump(CDumpContext& dc) const
{
    CDocument::Dump(dc);
}
#endif //_DEBUG

我该如何解决这个错误?

谢谢

答案

该错误告诉您构造函数Figure :: Figure()已被声明,但没有实现它。

此实现通常在.h文件附带的.cpp文件中捕获。您可以在其他类中看到此示例:

TetrisDoc.h :(声明)

class CTetrisDoc : public CDocument
{
protected: // create from serialization only
    CTetrisDoc();

TetrisDoc.cpp :(实现)

CTetrisDoc::CTetrisDoc()
{
    // TODO: add one-time construction code here

}

在我看来,图中的大多数函数只是声明,让我觉得你在项目中缺少对Figure.cpp的引用。即使它位于同一目录中,如果您的项目中没有对.cpp文件的引用,Visual Studio也无法找到它们,您将收到这些错误。

以上是关于vc ++ MFC应用程序,错误LNK2019:未解析的外部符号的主要内容,如果未能解决你的问题,请参考以下文章

在 VS2010 中,VC++ 错误 LNK 2019 with CoolProp 5.0.0

“错误 LNK2019:未解决...”

错误 LNK2019:未解析的外部符号 opencv

C++ LNK1120 和 LNK2019 错误:“未解析的外部符号 WinMain@16”

LNK2019:错误。使用 InternetOpen InternetReadFIle 的 C++ 程序中未解析的外部符号

链接 OpenCV 失败,LNK 2019 未解析的外部符号