c++ - 错误 C2248 : 'CObject::CObject' : cannot access private member declared in class 'CObject' afxwin. h

标签 c++ mfc

我正在尝试让类负责在灰色背景上放置一些文本:

得分.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
    };

分数.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);
}

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

最佳答案

CFont Class最终源自 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() 方法就是这样做的。您将不得不更改以返回引用或指针:

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

关于c++ - 错误 C2248 : 'CObject::CObject' : cannot access private member declared in class 'CObject' afxwin. h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28858590/

相关文章:

python - 为什么在 C++ 上使用 OpenCV 时的 CPU 使用率比在 Python 上高

C++ 比较一个字节和一个字母

mfc - 某些控件似乎没有随机绘制

c++ - 我可以从mfc中的另一个文档访问一个文档的数据吗

c++ - CMDIChildWndEx 框架中的 DockPaneLeftOf

c# - 桌面应用程序中的数据可视化

c++ - 如何获取进程内存的最小值和最大值?(c++)

c++ - 从没有对象的 Objective C 调用特定的 C++ 方法

c++ - 关于 operator new 重载和异常的问题

c++ - 如何在 MFC 中启动对话框?