错误 C2248:'CObject::CObject':无法访问在 class 'CObject' afxwin.h 中声明的私有成员

error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' afxwin.h

我正在努力让 class 负责在灰色背景上放置一些文字:

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);
}

关于这个错误,我只能说它应该在这个 class 中(错误指向 afhwin.h)。从我从其他问题中看到的情况来看,它通常与私有构造函数或字符串有关。这里的字符串属于 class,所以这应该不是问题,我不知道在哪里可以调用私有构造函数(目前这个 class 在其他 class 中没有对象)。请告诉我这里有什么问题。

您的 GetFont 函数 returns 按值传递字体,改为通过指针引用传递它。

CFont Class ultimately derives from the CObject Class。查看 CObject 的定义(参见 afx.h),您会发现以下注释:

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

换句话说:您不能按值传递 CObject 派生的对象。您的 Score::GetFont() 方法就是这样做的。您必须将引用或指针更改为 return:

const CFont& GetFont() { return m_Font; }
// or
const CFont* GetFont() { return &m_Font; }