[英]Passing CList variable gives error C2248: 'CObject::CObject' : cannot access private member
In My class, I've a static Clist variable
declared in the following way: 在我的课程中,我有一个静态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()
is not part of my class. otherfunc()
不属于我的课程。
Where am I going wrong? 我要去哪里错了? I have just pasted the code snippet with the problem. 我刚刚在代码片段中粘贴了该问题。 I have initiated it and everything works file except for the line where im calling otherfunc(). 我已经启动了它,并且一切正常,除了im调用otherfunc()所在的行之外,其他所有文件都有效。 Its has no dependence over static keyword. 它不依赖于静态关键字。 Even if i remove static, i get the same error. 即使我删除静态,我也会遇到相同的错误。
Edited : Here is the error tht I get : 编辑:这是我得到的错误:
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> ]
Your code as it is doesn't compile ( Class
should be class
, Public
should be public
etc). 您的代码无法编译( Class
应该是class
, Public
应该是public
等)。 What is the error message? 错误消息是什么? Also you must post a simple compilable example that reproduce your error. 另外,您必须发布一个简单的可编译示例来重现您的错误。 My guess is that you didn't instantiate your static variable outside its class declaration, see 我的猜测是您没有在类声明之外实例化静态变量,请参见
http://www.learncpp.com/cpp-tutorial/811-static-member-variables/ http://www.learncpp.com/cpp-tutorial/811-static-member-variables/
You may not get the error because of "Public:". 由于“ Public:”,您可能不会收到错误。 Because "Public:" is not a key word it's a label. 因为“ Public:”不是关键字,所以它是一个标签。 That's why "myvariable" is private by default. 这就是默认情况下“ myvariable”是私有的原因。 Instead of "Public:" use "public:" and also replace "Static" with static. 代替“ Public:”使用“ public:”,并用静态替换“ Static”。
Take a look at the definition of - 看一下-
void otherfunc(CList<int,int> a)
The input parameter CList<int,int> a
is passed by value, this means that when you call this function it will copy the input parameter by using CList<int,int>
Copy Constructor. 输入参数CList<int,int> a
通过值传递,这意味着调用此函数时,它将使用CList<int,int>
复制构造函数复制输入参数。
But CList<int,int>
does not implement a Copy Constructor, and its base class CObject
define its Copy Constructor as private. 但是CList<int,int>
没有实现Copy构造函数,并且它的基类CObject
将其Copy构造函数定义为私有。
You should change the definition to - 您应该将定义更改为-
void otherfunc(CList<int,int>& a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.