类不可访问,尽管包含
Class not accesible in spite of include
我对整个编程过程很陌生。尽管我认为我已经掌握了类和包括的基本概念,但我无法弄清楚我在这里做错了什么。我创建了自己的类,名为Grid,如下所示:
--- Grid.h ---
class Grid{
public:
Grid(HWND wnd);
public:
void paint(CDC &dc, int sqr, bool axis); //paint the grid
void tag(CDC &dc);
private:
int square; //square size
CRect frame; //client area size
};
--- Grid.cpp ---
#include "stdafx.h"
#include "Grid.h"
Grid::Grid(HWND wnd)
{
CRect rect;
GetClientRect(wnd, &rect); // get client area size
frame.left = rect.right / 2 - 387; // fit frame to margin
frame.right = frame.left + 774;
frame.top = rect.bottom - 874;
frame.bottom = rect.bottom - 100;
}
void Grid::paint(CDC &dc, int sqr, bool axis){
square = sqr; // paint grid
CPen penWhite;
penWhite.CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
dc.SelectObject(&penWhite);
dc.Rectangle(frame);
dc.MoveTo(frame.left + square, frame.top);
for (int i = 1; i < 18; i++){
dc.LineTo(frame.left + square * i, frame.bottom);
dc.MoveTo(frame.left + square + square * i, frame.top);
}
dc.MoveTo(frame.left, frame.top + square);
for (int i = 1; i < 18; i++){
dc.LineTo(frame.right, frame.top + square * i);
dc.MoveTo(frame.left, frame.top + square + square * i);
}
[...]
现在,我正在尝试做的是从另一个类添加此类的对象。正如你可能已经猜到的那样,它是一个 MFC 应用,应该利用我在 Grid 中提供的功能。因此我添加了
--- MainFrm.cpp ---
[...]
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
Grid theGrid{ GetSafeHwnd() };
}
CMainFrame::~CMainFrame()
{
}
[...]
我后来称之为:
void CMainFrame::OnPaint()
{
CPaintDC dc(this); // get device context for paint
// get current window size
CRect rect;
GetClientRect(&rect); // get current client area size
// fill background black
CBrush brushBlack;
brushBlack.CreateSolidBrush(RGB(0, 0, 0));
dc.SelectObject(&brushBlack);
dc.FillRect(rect, &brushBlack);
// paint grid
theGrid.paint(dc, 43, 1);
ReleaseDC(&dc);
} // end OnPaint()
。但是编译器给了我错误 C2065:"theGrid":未声明的标识符而Intellisense在theGrid.paint(dc,43,1)上咆哮;
我在这里做错了什么?我只想要一个从 MainFrm 中创建的 Grid 对象,每个函数都可以访问该对象。
亲切问候米琛
Grid theGrid{ GetSafeHwnd() };
在构造函数内部声明,因此仅获得本地范围。必须在构造函数外部声明它,并在构造函数上初始化它,如下所示:
--.h file--
public class CMainFrame{
Grid theGrid;
[...]
--.cpp file---
CMainFrame::CMainFrame() :
theGrid(GetSafeHwnd())
{}
在
MainFrm 的开头 #include"Grid.h.cpp
在 CMainFrame.h 文件中包含 Grid.h,并将 theGrid 定义为 CMainFrame 中的成员变量
在主Frm.h
#include "Grid.h"
...
class CMainFrame
{
....
Grid theGrid;
...
};
我主要.cpp
#inlcude "MainFrm.h"
...
CMainFrame::CMainFrame() : theGrid(GetSafeHwnd())
{
// TODO: add member initialization code here
}
相关文章:
- 读取大文件(>2GB)(文本文件包含以太网数据)并通过不同参数随机访问数据的最佳方法是什么?
- 派生的 wxPanel 控件如何访问其中包含 wxDialog 中的数据?
- 访问包含矢量对的映射的值
- 循环访问还包含未使用元素的字符串数组
- 如何使用包含内部类的类实例有效地从内部类访问成员?
- 访问包含P的有效索引时返回空格的C++字符串
- 如果包含引用成员的类中缺少原始变量,为什么它仍然可以访问?
- C/C++ 包含点的宏参数(成员访问运算符)
- 无法从多态嵌套类访问包含类的成员
- 我可以从C++访问包含QtObject的QQmlContext吗?
- 如何从另一个包含目录中的标头访问包含目录中的标头
- shared_ptr - 从包含的对象内部访问
- 访问其他抽象类,而无需在C 中包含参考
- 如何使用指向该数组的指针访问结构数组中包含的结构中的字段?[C++]
- 对象如何访问所包含的类的属性
- 如何访问C数组中包含的valarray中的元素
- 使用 JNA 访问包含 vector<char* 的结构>
- 在类组合中,使包含的类可以访问容器类变量
- Boost::p tree - 访问列表中包含的属性树节点
- 访问注册表数据以设置链接器和包含路径