简体   繁体   中英

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:

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

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(). 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). 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/

You may not get the error because of "Public:". Because "Public:" is not a key word it's a label. That's why "myvariable" is private by default. Instead of "Public:" use "public:" and also replace "Static" with 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.
But CList<int,int> does not implement a Copy Constructor, and its base class CObject define its Copy Constructor as private.

You should change the definition to -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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