CMYDoc* GetDocument() const;-错误

一、现象

在MDI工程中,新建一个新View类,CMyNewView,基类为CEditView。在CMyNewView::OnDraw(CDC* /*pDC*/)中设置指针后,弹出编译错误。

错误 1 error C2440: “初始化”: 无法从“CDocument *”转换为“CMyNewDoc *” 

二、思考

初步思考为文档和视图没有像关联。文档模块已经添加,难道还有什么地方需要相关联吗?

查找MDI框架自动生成代码,在MYView.h中发现这么一句:

public:
   CMyNewDoc* GetDocument() const;

估计问题出在这里,网上查找资料,添加相关代码。

编译,出现错误:

错误 1 error C2143: 语法错误 : 缺少“;”(在“*”的前面) 

错误 2 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 

......

漫长的折腾后,发现原因,#include 头文件顺序问题

三、总结

MFC内置的东西太多,不过这好像是对编译不了解。这个问题困扰了很长时间,贴出了给碰到同样的问题共享。

 

VC mfc文档中代码如下void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); for (int i=0;i<points.size();i++){ color=RGB(rand()%256,rand()%256,rand()%256); r=rand()%46+5; br.CreateSolidBrush(color); pDC->SelectObject(&br); pDC->Ellipse(points[i].x-r,points[i].y-r,points[i].x+r,points[i].y+r); br.DeleteObject(); } // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CMyView printing BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMyView diagnostics #ifdef _DEBUG void CMyView::AssertValid() const { CView::AssertValid(); } void CMyView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMyDoc* CMyView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return (CMyDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMyView message handlers void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default center=point; r=rand()%46+5;//r=5~50 color=RGB(rand()%256,rand()%256,rand()%256); points.push_back(center); SetTimer(1,200,NULL); CView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonUp(nFlags, point); } void CMyView::rise() { for(int i=0;i<points.size();i++) { points[i].y-=5; if(points[i].y<-r) { points.erase(points.begin()+i); i--; } } } void CMyView::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if(nIDEvent==1){ RedrawWindow(); rise(); } CView::OnTimer(nIDEvent); },运行效果中圆在上升过程中颜色和大小不停的变换,应怎么修改此代码使得圆在上升过程中的大小和颜色不会变换,完整步骤及代码
05-29
VC mfc文档中代码如下void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); for (int i=0;i<points.size();i++){ color=RGB(rand()%6,rand()%6,rand()%6); r=rand()F+5; br.CreateSolidBrush(color); pDC->SelectObject(&br); pDC->Ellipse(points[i].x-r,points[i].y-r,points[i].x+r,points[i].y+r); br.DeleteObject(); } // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CMyView printing BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMyView::OnBeginPrinting(CDC* /pDC/, CPrintInfo* /pInfo/) { // TODO: add extra initialization before printing } void CMyView::OnEndPrinting(CDC* /pDC/, CPrintInfo* /pInfo/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMyView diagnostics #ifdef _DEBUG void CMyView::AssertValid() const { CView::AssertValid(); } void CMyView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMyDoc* CMyView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return (CMyDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMyView message handlers void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default center=point; r=rand()F+5;//r=5~50 color=RGB(rand()%6,rand()%6,rand()%6); points.push_back(center); SetTimer(1,200,NULL); CView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonUp(nFlags, point); } void CMyView::rise() { for(int i=0;i<points.size();i++) { points[i].y-=5; if(points[i].y<-r) { points.erase(points.begin()+i); i--; } } } void CMyView::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if(nIDEvent==1){ RedrawWindow(); rise(); } CView::OnTimer(nIDEvent); },怎么修改此代码能实现每次单击鼠标出现的圆大小和颜色随机,但在圆的上升过程中大小和颜色都为刚开始单击鼠标时的大小和颜色,不会不停的变换。并且前面单击鼠标出现的圆的颜色和大小不会随着后面单击鼠标出现的圆的颜色和大小改变而改变。给出完整步骤及代码
最新发布
05-29
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值