C++ “错误 C2248:‘CObject::CObject’:无法访问类‘CObject’中声明的私有成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/865035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:39:00  来源:igfitidea点击:

"error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'

c++mfc

提问by Attilah

Possible Duplicate:
error using CArray

可能的重复:
使用 CArray 时出错

Duplicate : error using CArray

重复:使用 CArray 时出错



so, i am trying to use CArray like this :

所以,我想像这样使用 CArray:

   CArray<CPerson,CPerson&> allPersons;
   int i=0;
   for(int i=0;i<10;i++)
   {
      allPersons.SetAtGrow(i,CPerson(i));
      i++;
   }

but when compiling my program, i get this error :

但是在编译我的程序时,我收到此错误:

"error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxtempl.h"

I don't even understand where this is coming from.

我什至不明白这是从哪里来的。

HELP !

帮助 !

回答by Gixxernaut

The problem is that you're constructing a CObject on the stack. Somewhere in your program you're attempting to pass a reference to a CArray object but you accidentally left out the "&" in the function prototype. For example:

问题是您正在堆栈上构造一个 CObject。在程序中的某个地方,您试图传递对 CArray 对象的引用,但您不小心遗漏了函数原型中的“&”。例如:

void DoFoo(CArray cArr)
{
    // Do something to cArr...
}

^^^ The code above will cause the error you're having.

^^^ 上面的代码会导致您遇到的错误。

void DoFoo(CArray & cArr)
{
    // Do something to cArr...
}

^^^ The code above will not cause the problem.

^^^ 上面的代码不会导致问题。

回答by Gixxernaut

Write a constructor for your class (CPerson) and make it public. it should solve the problem.

为您的类 (CPerson) 编写一个构造函数并将其公开。它应该可以解决问题。

回答by Daniel Earwicker

It means that your program is trying to construct an instance of CObject, which appears to be banned because CObjecthas a private constructor.

这意味着您的程序正在尝试构造 的实例CObject,该实例似乎被禁止,因为CObject它具有私有构造函数。

Maybe the CArrayis trying to construct those instances? What does the rest of the program look like?

也许CArray正试图构建这些实例?程序的其余部分是什么样的?