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

时间:2015-03-04 15:41:39

标签: c++ mfc

我试图让班级负责将一些文字放在灰色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);
}

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

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

您的GetFont函数按值返回COunt,而是通过引用传递它。