none
error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' RRS feed

  • Question

  •  

    hi all

     

    i have got the following error

     

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

     

    this is my class

     

    #pragma once

    #include "Definition.h"

    class CLexicalAnalyzer

    {

    public:

    CLexicalAnalyzer(void);

    public:

    ~CLexicalAnalyzer(void);

    public:

    int lex();

    public:

    void getIdlFile(CFile IdlFile);

    void verify(char token[50]);

    char*deleteComments(char buff[]);

    public:

    struct SymbolTable m_SymbolTable[50];

    int CountOfSymbolTable;

    char *m_IdlFileBuffer;

    };

     

     

     

     

    i have created an object of this class in another class and tried to access getIdlFile(CFile IdlFile); method

    but the error

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

     

     

    can any body tell me why its happening ??

     

    thanks in advance...

    Tuesday, March 18, 2008 4:20 AM

Answers

  • CFile is derived from CObject.  CObject declares a private copy constructor, so that constructor can't be used by default unless it's implemented in a derived class.

    When you pass an object as parameter by value, as you are, C++ must be able to call the copy constructor in order to create the copy to pass to the function.  That's why you get the error when you try to call getIdlFile.

    As Viorel suggested, if you need to modify the passed CFile, modify the function to use pass-by-reference: getIdlFile(CFile &IdlFile);

    If you don't, have it pass as a const reference: getIdlFile(const CFIle &IdlFile);

    Tuesday, March 18, 2008 1:18 PM
  • It seems the getIdlFile(CFile IdlFile) function has to be transformed to getIdlFile(const CFile & IdlFile) or getIdlFile(CFile & IdlFile). I hope this helps.

     

    Tuesday, March 18, 2008 7:05 AM

All replies

  • Where is CObject defined? What line does your error message refer to?

     

     

    Tuesday, March 18, 2008 6:19 AM
  • It seems the getIdlFile(CFile IdlFile) function has to be transformed to getIdlFile(const CFile & IdlFile) or getIdlFile(CFile & IdlFile). I hope this helps.

     

    Tuesday, March 18, 2008 7:05 AM
  • i have created an object of CLexicalAnalyser class in another class,

     

    which is Derived from CDialog

     

     and tried to access the method 

     

    getIdlFile(CFile IdlFile);

     

    but the error

     

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

     

    but as soon as i ve changed the parameter CFile IdlFile to a char * its working i still having truble

     

    understanding why CFile object is not allowed while char* is working properly...

     

     

     

     

     

    Tuesday, March 18, 2008 7:05 AM
  • CFile is derived from CObject.  CObject declares a private copy constructor, so that constructor can't be used by default unless it's implemented in a derived class.

    When you pass an object as parameter by value, as you are, C++ must be able to call the copy constructor in order to create the copy to pass to the function.  That's why you get the error when you try to call getIdlFile.

    As Viorel suggested, if you need to modify the passed CFile, modify the function to use pass-by-reference: getIdlFile(CFile &IdlFile);

    If you don't, have it pass as a const reference: getIdlFile(const CFIle &IdlFile);

    Tuesday, March 18, 2008 1:18 PM
  • By default,all class members are private in C++. If you don't declare "public:" before constructor, you'll get same error whenever you try to create an object of that class. 
    Thursday, June 7, 2012 7:08 AM