我如何初始化一个CARRAY&LT的默认值; CClass *>用空CARRAY函数参数? [英] How can I initialize the default value of a CArray<CClass*> function parameter with an empty CArray?

查看:286
本文介绍了我如何初始化一个CARRAY&LT的默认值; CClass *>用空CARRAY函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以用的std ::矢量更好地做到这一点,但我搞乱的应用,已经有一堆 CARRAY <的对很多相关的功能/ code>参数......而此刻我不会改变他们!

I know I could do this better with std::vector, but the application I am messing with, has already a bunch of CArray parameters on a lot of related functions ... and I will not change them all at the moment!

我只是想定义一个空的 CARRAY&LT; CClass *&GT; - 阵列的指针 CClass ,所以问题的不能 CClass 构造函数 - 作为函数参数的默认值

I simply want to define an empty CArray<CClass*> — array of pointers to CClass, so the problem can not be on the CClass constructor — as the default value of a function parameter.

方法1

如果我尝试的赋值运算符的做出来,的默认的构造函数的:

If I try to do it with assignment operator and default constructor:

void Function(CArray<CClass*> parameter = CArray<CClass*>());

我得到的错误:

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxTempl.h(262): error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\afx.h(535) : see declaration of 'CObject::CObject'
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\afx.h(510) : see declaration of 'CObject'
1>          This diagnostic occurred in the compiler generated function 'CArray<TYPE>::CArray(const CArray<TYPE> &)'
1>          with
1>          [
1>              TYPE=CClass *
1>          ]

方法2

我也试过用的拷贝构造函数的:

void Function(CArray<Class*> parameter(CArray<CClass*>()));

我得到了错误:

 >File.cpp(line X): error C2664: 'FunctionClass::Function' : cannot convert parameter 1 from 'CArray<TYPE>' to 'CArray<TYPE> (__cdecl *)(CArray<TYPE> (__cdecl *)(void))'
1>          with
1>          [
1>              TYPE=CClass*
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


  • 行X:包含一个呼叫提供的参数功能,如下所示: pFunctionClass-&GT;功能(参数);

    • line X: contains a call supplying the parameter to Function, as shown: pFunctionClass->Function(parameter);
    • 1> CFunctionClass.cpp(行Y):错误C2511:无效CFunctionClass ::功能(CARRAY)':重载成员函数不是在'CShoePlaceDoc发现
      1>
      1> [
      1> TYPE = CClass *
      1>]
      1> FunctionClass.h(A线):见CFunctionClass

      1>CFunctionClass.cpp(line Y): error C2511: 'void CFunctionClass::Function(CArray)' : overloaded member function not found in 'CShoePlaceDoc' 1> with 1> [ 1> TYPE=CClass* 1> ] 1> FunctionClass.h(line A) : see declaration of 'CFunctionClass'


      • 线Y包含功能实施的头,如图所示:`无效CFunctionClass ::功能(CARRAY参数)

      • line Y contains the Function implementation header, as shown: `void CFunctionClass::Function(CArray parameter)

      1> File.cpp(线Z):错误C2660:'CClassFunction ::功能':函数不接受0参数

      1>File.cpp(line Z): error C2660: 'CClassFunction::Function' : function does not take 0 arguments


      • 行Z:包含功能呼叫而不提供它的参数,如下所示: pClassFunction-&GT;功能();

      • line Z: contains a call to Functionwithout supplying it parameters, as shown: pClassFunction->Function();

      该方法没有工作,但它朝着的结论得到了它的方式:这是不可能的使用拷贝构造为函数参数指定一个默认值

      The approach didn't work, but it got its way towards a conclusion: It is not possible to use a copy-constructor for assigning a default value for a function parameter.

      方法3

      如果我试图用的的λ的:

      void Function(CArray<CClass*> parameter = [] () -> CArray<CClass*>{ return CArray<CClass*> (); } );
      

      ,那么我会得到这两个错误的多个输出:

      , then I will get multiple outputs of these two errors:

      1>FunctionClass.h(line A): error C2440: 'default argument' : cannot convert from '`anonymous-namespace'::<lambda2>' to 'CArray<TYPE>'
      1>          with
      1>          [
      1>              TYPE=CClass*
      1>          ]
      1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
      1>FunctionClass.h(line B): fatal error C1903: unable to recover from previous error(s); stopping compilation
      


      • A线:方法声明

      • B线:收} FunctionClass 包含类功能

        • line A: method declaration
        • line B: closing } of FunctionClass class that contains Function method
        • 问题的原产地

          问题的根源似乎是一个事实,即 CARRAY 的CObject 直接派生的类,该声明赋值运算符为​​私有

          The root cause of the problem seems to be the fact that CArray is a class directly derived from CObject, which declares the assignment operator as private:

          class AFX_NOVTABLE CObject
          {
          //...
          private:
              CObject(const CObject& objectSrc);              // no implementation
              void operator=(const CObject& objectSrc);       // no implementation
          //...
          }
          

          那么,如何可以提供一个空数组作为参数的默认值?

          So, how can I supply an empty array as default value for the parameter?

          推荐答案

          通过这个声明

          void Function(CArray<CClass*> parameter /*...*/);
          

          您不能。调用此函数将调用的CObject 的私人拷贝构造函数,你已经注意到了。

          You can't. Calling this function will invoke the private copy constructor of CObject as you have noticed.

          你可以做什么,是添加静态CARRAY℃的对象; CClass *&GT; 在你的类和对它的引用初始化函数。这样,这将是空的(只要你不填充...)的,你可以执行的 .IsEmpty() 检查就可以了。

          What you could do, is to add a object of static CArray<CClass*> in your class and initialize the function with a reference to it. This way it will be empty (as long as you do not populate it...) and you can perform a .IsEmpty() check on it.

          private:
              static CArray<CClass*> myObj;
          //...
          void Function(CArray<CClass*> &parameter = myObj);
          

          或者,它初始化为0,这样,您只需通过如果(参数)如果(NULL ==参数)

          void Function(CArray<CClass*> *parameter = NULL);
          

          这篇关于我如何初始化一个CARRAY&LT的默认值; CClass *&GT;用空CARRAY函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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