使用MFC中的成员初始化列表的构造函数的初始化顺序 [英] Order of Initialization of constructors using member initialization lists in MFC

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

问题描述

查看更多:MFC

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

See more: MFC 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
CDlgMac : public CDialog
{
// Construction
public:
//  CDlgMac (CMac*,CWrkShp*,CWnd* pParent = NULL);   // Existing Standard(Signature 1) constructor -> Scenario-I sucessfully builds with no errors.
CDlgMac (CMac*,CWrkShp*,CPtrList& ,CJoe&,CWnd* pParent = NULL); //Signature 2 on including Class Job and Pointer in Constructor the error is depicted.



似乎这是构造函数的典型处理方式,我想我在成员初始化列表中缺少一些地方.下面在这里添加了两个新的数据成员



Seems this is typical handling for constructors, I think I am missing some where in member initialization lists.Two new data members get added here below

CJoe& m_Opt;
CPtrList& m_Obj; 



包含以上内容之后,相同的构造函数定义如下,
在Dialog.cpp

//带有Dialog.h文件的较早的现有构造方法定义,(签名1)



After above inclusion, the same constructor definition are as under,
In Dialog.cpp

//Earlier Existing Constructor definition w.r.t Dialog.h file,(signature 1)

CDlgMac ::CDlgMac (CMac* pMac,CWrkshp* pWkshp,CWnd* pParent /*=NULL*/)
 : CDialog(CDlgMac ::IDD, pParent), m_pMac(pMac),m_pWorkShop(pWrkShp)
{
//{{AFX_DATA_INIT(CDlgMac )
// NOTE: the ClassWizard will add member initialization here
 //}}AFX_DATA_INIT
}


修改的构造函数定义如下,


modified constructor definition as below,

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中Job.cpp

我调用以下代码,



In OtherFile Job.cpp

I invoke the following code,

...
...
CDlgMac dlg(pMac,m_pParent->GetWork(),NULL); ----> Error shown in this line



在构建时,出现以下错误,

1>错误C2664:``CDlgMac :: CDlgMac(CMac *,CWrkShp *,CPtrList&,CJoe&,CWnd *)'':无法将参数3从``int''转换为``CPtrList&''



2> d:\ v-olp \ Job.cpp(1906):错误C2668:``CDlgMac :: CDlgMac'':对重载函数的模糊调用


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

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

问候,
VishalK



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 &''

OR

2>d:\v-olp\Job.cpp(1906) : error C2668: ''CDlgMac::CDlgMac'' : ambiguous call to overloaded function


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

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

With Regards,
VishalK

推荐答案

好吧,错误消息似乎很清楚:第三个参数应该是对CPtrList的引用,并且您传递了NULL.引用永远不能为NULL;它不像一个指针.因此,编译器猜测NULL(定义为0)是一个整数,无法将其转换为引用.

如果希望第三个参数是可选的,则将其定义为指针而不是引用,然后可以将此参数的默认值设置为NULL.会起作用.
Well, the error message seems clear: The third parameter is supposed to be a reference to CPtrList and you passed NULL. A reference can never be NULL; it''s not like a pointer. Hence, the compiler guesses that NULL (defined is 0) is an integer and that cannot be converted to a reference.

If your want the third parameter to be optional, define it as a pointer and not a reference and then you can give this parameter a default value of NULL. That will work.


这已经向您解释了 ^ ].如果要允许参数为NULL,则它不能作为引用.鉴于您要执行的操作,对所有参数使用指针比在尝试进行混合时更明智.
This has already been explained to you in your previous question[^]. If you are going to allow a parameter to be NULL then it cannot be a reference. Given what you are trying to do it would be much more sensible to use pointers for all parameters rather than mixing them as you are trying to do.


还请注意,您必须遵守顺序初始化成员的方式与声明成员的方式相同:
由于:
Also notice that you must respect the order of initialisaton the same way as you declare the members :
since :
CJoe& m_Opt;
CPtrList& m_Obj;



以下也不正确:



following is not correct either :

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
}....
....



因为m_Opt应该在m_Obj之前.



because m_Opt should come before m_Obj.


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

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