问题描述
我试图让班级负责在灰色背景上放置一些文字:
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);
}
关于这个错误,我只能说它应该在这个类中(错误指向 afhwin.h).从我从其他问题中看到的情况来看,它通常与私有构造函数或字符串有关.这里的字符串属于类,所以它不应该是问题,我不知道在哪里可以调用私有构造函数(现在这个类在其他类中没有对象).请告诉我这里有什么问题.
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.
推荐答案
CFont类 最终派生自 CObject 类.查看 CObject
的定义(参见 afx.h),您会发现以下注释:
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.
换句话说:您不能通过值传递 CObject
派生的对象.您的 Score::GetFont()
方法就是这样做的.您必须更改以返回引用或指针:
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; }
这篇关于错误 C2248:“CObject::CObject":无法访问类“CObject"afxwin.h 中声明的私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!