使用成员初始化列表,使用类,指针等初始化构造函数 [英] Initialisation of constructors using class, pointers, etc., using member initialisation lists

查看:345
本文介绍了使用成员初始化列表,使用类,指针等初始化构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的基于Dialog的MFC应用程序中,我有以下错误w.r.t构造函数,即使按顺序进行了多次更改以及参数的类型,错误w.r.t构造函数仍然存在,

//Dialog.h

Hi,

In my Dialog based MFC application, I have the below error w.r.t Constructor, even though multiple changes made in order, and types of params, the error w.r.t constructor still exists,

//Dialog.h

class CDlgMac : public CDialog
{
// Construction
public:
//  CDlgMac (CMac*,CWrkShp*,CWnd* pParent = NULL);   // standard constructor -> Scenario-I sucessfully builds with no errors.
CDlgMac (CMac*,CWrkShp*,CPtrList& ,CJoe&,CWnd* pParent = NULL); //stand

->关于在构造函数中包含类Joe和Pointer的方案-II,描述了错误.


-> Scenario -II on including Class Joe and Pointer in Constructor the error is depicted.


CJoe& m_Opt;
    CPtrList& m_Obj;



在Dialog.cpp


//在此处添加CNewClass头文件.
#include"NewClass.h"

方案I:

//CDlgMac :: CDlgMac(CMachine * pMachine,CWrkshp * pWkshp,CWnd * pParent/* = NULL */)
//:CDialog(CDlgMac :: IDD,pParent),m_pMachine(pMachine),m_pWorkShop(pWorkShop)
{
//{{AFX_DATA_INIT(CDlgMac)
//注意:ClassWizard将在此处添加成员初始化
////}} AFX_DATA_INIT
}
方案II:



In Dialog.cpp


//Adding CNewClass header file here.
#include "NewClass.h"

Scenario-I:

//CDlgMac ::CDlgMac (CMachine* pMachine,CWrkshp* pWkshp,CWnd* pParent /*=NULL*/)
// : CDialog(CDlgMac ::IDD, pParent), m_pMachine(pMachine),m_pWorkShop(pWorkShop)
{
//{{AFX_DATA_INIT(CDlgMac )
// NOTE: the ClassWizard will add member initialization here
// //}}AFX_DATA_INIT
}
Scenario-II:

CDlgMac::CDlgMac(CMac* pMac,CWrkShp* pWrkShp,CPtrList& ObjectLst,CJoe& OptPage2 ,CWnd* pParent /*=NULL*/)
    : CDialog(CDlgMac::IDD, pParent), m_pMac(pMac),m_pWrkShp(pWrkShp),m_Obj(ObjectLst),m_Opt(OptPage2)
{
    //{{AFX_DATA_INIT(CDlgMac)
        // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT
}


....
....

在OtherFile中James.cpp

我调用以下代码,
...
...
CDlgMac dlg(pMac,m_pParent-> GetWork(),NULL); ---->该行显示错误

在构建时,出现以下错误,
1>错误C2664:``CDlgMac :: CDlgMac(CMac *,CWrkShp *,CPtrList&,CJoe&,CWnd *)'':无法将参数3从``int''转换为``CPtrList&''

基本上,我的目标是使用成员初始化列表,指针和类(引用)来初始化所有变量/数据成员.

解决上述问题的任何帮助将非常可观.,
我正在使用VS2008IDE,MFC 9.0v.

问候,
VishalK


....
....

In OtherFile James.cpp

I invoke the following code,
...
...
CDlgMac dlg(pMac,m_pParent->GetWork(),NULL); ----> Error shown in this line

On build, I get the following errors,
1>error C2664: ''CDlgMac::CDlgMac(CMac *,CWrkShp *,CPtrList &,CJoe &,CWnd *)'' : cannot convert parameter 3 from ''int'' to ''CPtrList &''

Basically, my target is to initialize all the variables/datamembers using member initialisation lists, using pointer and class(reference).

Any help in resolving the above would be much appreciable.,
I am using VS2008IDE, MFC 9.0v.

With Regards,
VishalK

推荐答案

编译器当然是正确的.您不能使用NULL初始化引用(PtrList& ObjectLst).
The compiler, of course is right. You cannot initialise a reference (PtrList& ObjectLst) with NULL.


问题出现在list_p.cpp
The problem raises in the definition of the class CPtrList in list_p.cpp
CPtrList::CPtrList(INT_PTR nBlockSize)
{
	ASSERT(nBlockSize > 0);

	m_nCount = 0;
	m_pNodeHead = m_pNodeTail = m_pNodeFree = NULL;
	m_pBlocks = NULL;
	m_nBlockSize = nBlockSize;
}


您可以清楚地看到不能为类型CPtrList的变量赋予NULL值
请参阅 CObList :: CObList
我知道其有关其他构造函数的信息,但该类与CPtrList完全相同,在msdn中,此分类没有得到很好的记录

CPtrList类
希望这些信息对您有帮助


You can clearly see you cant give a value NULL to a variable of type CPtrList
Please see CObList::CObList
I know its information about other constructor but this class its almoust indentical to the CPtrList and in msdn this clas isnt documented well

CPtrList Class
I hope this information helps you


这篇关于使用成员初始化列表,使用类,指针等初始化构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆