繁体   English   中英

传递CList变量会给出错误C2248:'CObject :: CObject':无法访问私有成员

[英]Passing CList variable gives error C2248: 'CObject::CObject' : cannot access private member

在我的课程中,我有一个静态Clist variable ,它通过以下方式声明:

#include<stdio.h>
#include<conio.h>
#include <afxtempl.h>
void otherfunc(CList<int,int> a)
{

}
class A
{
public:
CList<int,int> myvariable;
void myfunc()
{
otherfunc(myvariable);
}

};


int _tmain(int argc, _TCHAR* argv[])
{
    A a;
    a.myfunc();
    getch();
    return 0;
}

otherfunc()不属于我的课程。

我要去哪里错了? 我刚刚在代码片段中粘贴了该问题。 我已经启动了它,并且一切正常,除了im调用otherfunc()所在的行之外,其他所有文件都有效。 它不依赖于静态关键字。 即使我删除静态,我也会遇到相同的错误。

编辑:这是我得到的错误:

C:\program files (x86)\microsoft visual studio 9.0\vc\atlmfc\include\afxtempl.h(776) : error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\atlmfc\include\afx.h(561) : see declaration of 'CObject::CObject'
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\atlmfc\include\afx.h(532) : see declaration of 'CObject'
1>        This diagnostic occurred in the compiler generated function 'CList<TYPE,ARG_TYPE>::CList(const CList<TYPE,ARG_TYPE> &)'
1>        with
1>        [
1>            TYPE=int,
1>            ARG_TYPE=int
1>        ]

您的代码无法编译( Class应该是classPublic应该是public等)。 错误消息是什么? 另外,您必须发布一个简单的可编译示例来重现您的错误。 我的猜测是您没有在类声明之外实例化静态变量,请参见

http://www.learncpp.com/cpp-tutorial/811-static-member-variables/

由于“ Public:”,您可能不会收到错误。 因为“ Public:”不是关键字,所以它是一个标签。 这就是默认情况下“ myvariable”是私有的原因。 代替“ Public:”使用“ public:”,并用静态替换“ Static”。

看一下-

void otherfunc(CList<int,int> a)

输入参数CList<int,int> a通过值传递,这意味着调用此函数时,它将使用CList<int,int>复制构造函数复制输入参数。
但是CList<int,int>没有实现Copy构造函数,并且它的基类CObject将其Copy构造函数定义为私有。

您应该将定义更改为-

void otherfunc(CList<int,int>& a)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM