3

I'm trying to make class responsible for putting some text on grey bg:

Score.h

#pragma once
class Score
{
public:
    Score();
    ~Score();
    void UpdateScore(int points);
    void UpdateLives(int lives);
    int GetScore(){ return m_iScore; }
    int GetLives(){ return m_iLives; }
    void GetScoreText();//CString 
    void GetLivesText();
    CRect GetArea(){ return m_Area; }
    void SetArea(int MaxWidth, int MaxHeight, int Width);
    void DrawScore(CDC* pDC);
    CPoint GetText1Area(){ return m_ptText1; }
    CPoint GetText2Area(){ return m_ptText2; }
    COLORREF GetText1Color(){ return COLOR_TXT1; }
    COLORREF GetText2Color(){ return COLOR_TXT2; }
    COLORREF GetScoreColor(){ return GREY; }
    CFont GetFont(){ return m_Font; }

private:
        CRect m_Area;
        CPoint m_ptText1;
        CPoint m_ptText2;
        int m_iScore;
        int m_iLives;
        CString m_sScore;
        CString m_sLives;
        CFont m_Font;
        const COLORREF COLOR_TXT1 = RGB(0, 255, 127);//lives txt color
        const COLORREF COLOR_TXT2 = RGB(50, 205, 50);//score txt color
        const COLORREF GREY = RGB(128, 128, 128);// bg color
    };

Score.cpp

#include"stdafx.h"

Score::Score()
{
    m_iScore = 0;
    m_iLives = 1;
    m_Font.CreateFont(
        12,                        // nHeight
        0,                         // nWidth
        0,                         // nEscapement
        0,                         // nOrientation
        FW_NORMAL,                 // nWeight
        FALSE,                     // bItalic
        FALSE,                     // bUnderline
        0,                         // cStrikeOut
        ANSI_CHARSET,              // nCharSet
        OUT_DEFAULT_PRECIS,        // nOutPrecision
        CLIP_DEFAULT_PRECIS,       // nClipPrecision
        DEFAULT_QUALITY,           // nQuality
        DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
        _T("Arial"));                 // lpszFacename 
}

Score::~Score()
{
    m_Font.DeleteObject();
}
void Score::GetScoreText()
{
    char c[20];
    sprintf_s(c, "Score: %d", m_iScore);
    m_sScore.Format(_T("%S"), c);
    //return m_sScore;
}
void Score::GetLivesText()
{
    char c[20];
    sprintf_s(c, "Lives: %d", m_iLives);
    m_sLives.Format(_T("%S"), c);
    //return m_sLives;
}
void Score::SetArea(int MaxWidth, int MaxHeight, int Width)
{
    m_Area = CRect(Width, 0, MaxWidth, MaxHeight);
    m_ptText1 = CPoint(static_cast<int>(Width * 1.1), static_cast<int>(MaxHeight * 0.1));
    m_ptText2 = CPoint(static_cast<int>(Width * 1.1), static_cast<int>(MaxHeight * 0.2));
}
void Score::DrawScore(CDC* pDC)
{
    GetLivesText();
    GetScoreText();
    CPen pen2(PS_SOLID, 0, GREY);
    CBrush brush2(GREY);
    CPen* pOldPen = pDC->SelectObject(&pen2);
    CBrush* pOldBrush = pDC->SelectObject(&brush2);
    pDC->Rectangle(m_Area);
    pDC->SelectObject(pOldPen); //resetting default Pen
    pDC->SelectObject(pOldBrush); //resetting default Brush
    CFont *pPrevFont = pDC->SelectObject(&m_Font);
    pDC->SetTextColor(COLOR_TXT1);
    pDC->TextOut(m_ptText1.x, m_ptText1.y, m_sLives);
    pDC->SetTextColor(COLOR_TXT2);
    pDC->TextOut(m_ptText2.x, m_ptText2.y, m_sScore);
    pDC->SelectObject(pPrevFont);
}

All I can tell about this error is that it should be in this class(error points to afhwin.h). From what I saw from other questions it's usually connected to private constructor or strings. Here strings belong to class so it shouldn't be problem and I can't tell where may be called private constructor(for now this class have no obiects in other classes). Please tell me what's wrong here.

mielu
  • 63
  • 2
  • 8

2 Answers2

7

The CFont Class ultimately derives from the CObject Class. Looking at CObject's definition (see afx.h) you will find the following comment:

// Disable the copy constructor and assignment by default so you will get
//   compiler errors instead of unexpected behaviour if you pass objects
//   by value or assign objects.

In other words: You cannot pass CObject-derived objects by value. Your Score::GetFont() method is doing just that. You will have to change to return either a reference or pointer:

const CFont& GetFont() { return m_Font; }
// or
const CFont* GetFont() { return &m_Font; }
IInspectable
  • 46,945
  • 8
  • 85
  • 181
2

Your GetFont function returns a CFont by value, pass it by refernce of pointer instead.

sithereal
  • 1,656
  • 9
  • 16