CTestView是创建工程时的视类,拆分窗体产生新类CRightView
我在文档类中这样写:
CView* CTestDoc::GetView(CRuntimeClass* pClass) //自定义成员函数,寻找视类指针
{ CView* pView;  POSITION pos=GetFirstViewPosition();  while(pos!=NULL)
{  pView=GetNextView(pos);  if(!pView->IsKindOf(pClass))  break;
}  if(!pView->IsKindOf(pClass))
{  AfxMessageBox(_T("Can not Locate the View!")); 
return NULL;
}  return pView;
}在文档类的按钮响应函数中写:
CTestView* pView=(CTestView*)GetView(RUNTIME_CLASS(CTestView));
pView->m_no; // m_no是CTestView中成员变量
运行后点击按钮后先是提示“Can not Locate the View!”,接着发生中断操作的错误,错误指向了
CTestDoc* CTestView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)));
return (CTestDoc*)m_pDocument;
}请教高手如何解决才能正确调用视类中的成员变量(函数)??

解决方案 »

  1.   

    运行后点击按钮后先是提示“Can not Locate the View!”,那么return NULL,CTestView* pView=NULL;错误的指针调用pView->m_no;
    你看看这里是不是错了:
    while(pos!=NULL)
    { pView=GetNextView(pos); if(!pView->IsKindOf(pClass)) \\应该是if(pView->IsKindOf(pClass)) 
    break;
    }
      

  2.   

    yxz_lp() 是正解,写错了,汗。。
      

  3.   

    LS强,!号也能看出来.
    另:
    CTestView* pView=(CTestView*)GetView(RUNTIME_CLASS(CTestView));
    pView->m_no;
    对指针操作最好都做下判断或断言什么的.虽然你里面判断了,但实际使用的地方并没有,所以.....