-1

I have a mysterious (at least to me) error LNK2019: unresolved external symbol , when compiling this game example in visual studio vc++.

Full error:

Error error LNK2019: unresolved external symbol "public: __thiscall Figure::Figure(void)" (??0Figure@@QAE@XZ) referenced in function "protected: __thiscall CTetrisDoc::CTetrisDoc(void)" (??0CTetrisDoc@@IAE@XZ) C:\Users\ary\Documents\Visual Studio 2013\Projects\Tetris\Tetris\TetrisDoc.obj Tetris

Figure header:

#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 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.cpp file:

// 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

How can I resolve this error?

Thanks

cyber101
  • 2,822
  • 14
  • 50
  • 93
  • 1
    Methods of `Figure` class are not implemented anywhere. – Igor Tandetnik Mar 02 '18 at 05:45
  • ...or the file containing the methods of `Figure` is not part of the project. – Jabberwocky Mar 02 '18 at 07:13
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – IInspectable Mar 02 '18 at 09:09

1 Answers1

1

The error is telling you that the constructor Figure::Figure() has been declared but there is no implementation of it.

This implementation usually is captured in the .cpp file that accompanies the .h file. You can see an example of this in your other class:

TetrisDoc.h: (declaration)

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

TetrisDoc.cpp: (implementation)

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

}

It looks to me that most of the functions in Figure are only declared, making me think that you're missing a reference to Figure.cpp in your project. Even if it's in the same directory, if you don't have a reference to your .cpp files in your project Visual Studio won't find them and you'll get these errors.

Bales
  • 58
  • 1
  • 5