yet another c2248 error

I'm another user (paid ) getting this compile error message now that the project was converted from .net 2003 to .net 2005


this is a show stopper and I need a resolution of some kind so any ideas /help would be appreciated .

C:\Program Files (x86)\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(879) : error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
        C:\Program Files (x86)\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(539) : see declaration of 'CObject::CObject'
        C:\Program Files (x86)\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(510) : see declaration of 'CObject'
        This diagnostic occurred in the compiler generated function 'CException::CException(const CException &)'



Answer this question

yet another c2248 error

  • Computerman45

     Jonathan Caves MSFT wrote:
    If this was allowed in previous versions of Visual C++ then it was due to a bug in the compiler which has been fixed in the Whidbey release.

    Is there any compiler option to revert back to the old behavior  

    How do you go about finding these types of errors   It's really hard to get motivated when you have literally thousands of them in a large project.  Is there some common denominator to them   I've yet to find a simple solution.

  • yarrick

    I got this type of error when a tried to pass a CStringList object into a class constructor.

    To resolve it I just passed in a CStringList pointer instead and things worked fine.


  • FABIOG

    I suspect that the problem here is that CObject now has a private copy-constructor and assignment-operator and somewhere in the code there is a class, let's call it X, that either inherits (directly or indirectly) from CObject or has an instance member that inherits (directly or indirectly) from CObject.

    The user is then trying to create a copy of an instance of this type X which causes the compiler to try to instantiate a previously unused copy-constructor or assignment-operator and while doing this it hits the private method in CObject and this is what causes the error.

    There are many ways to hit this problem but the code below demonstrates the general problem and how it may not be immediately obvious:

    class CObject
    {
    private
    :
       CObject(
    const
    CObject&);
       CObject&
    operator=(const
    CObject&);
    };

    class B : public CObject
    {
    };

    class D : public B
    {
    };

    class X : public D
    {
    };

    void f(const X& x1)
    {
       X x2 = x2; // Try to create a copy of X
    }




  • Sowmy Srinivasan

    No: there is no way to revert to the previous behavior. These errors are usually due to some common base class, like CObject, that is not meant to be copied and thus has a private copy-constructor and/or assignment-operator.

  • Victor Urnyshev

    I am getting the same error when I throw a class derived from CException using:

    throw xMsg( "Error" );

    It goes away when I change each throw to:

    throw new xMsg( "Error" );

    However, my catch statements are:

    try {
    ...
    }
    catch ( xMsg &X ) {
       cerr << X.Why() << endl;
    }

    This causes two questions.

    1) Why does "throw xMsg(...)" cause the copy constructor or operator to be called
    2) If I add the "new" to all of the "throw" statements, do I also have to modify all of the catch statements
     
    Please keep in mind that I have 250k lines in my library.

  • Isisha

    How about AfxThrowFileException()

    Somebody forgot to fit it It produces the same error message.


  • gabrielh

    If this was allowed in previous versions of Visual C++ then it was due to a bug in the compiler which has been fixed in the Whidbey release.

    The intention of having the private copy-constructor and the private assignment-operator in the CObject class is that this class (and classes derived from this class) should not be copyable. So this is a feature of the design of MFC that you should not be trying to get around.

  • TomasDeml

    I think MFC 2003 is the same, ie the afx.h for CObject is the same, but it works in VS 2003.net os is this a compiler issue

    Or is there a way to get around this


  • arsalan

    Hi,

    I am converting our application from VC++ 6.0 to VS 2005. I encountered the same problem. My object inherited from CException:

    Code Snippet

    class CMyException : public CException

    {

    public:

    CMyException(int i);

    }

    When I have this statement:

    Code Snippet

    throw CMyException(1);

    I got:

    d:\program files\microsoft visual studio 8\vc\atlmfc\include\afx.h(893) : error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'

    d:\program files\microsoft visual studio 8\vc\atlmfc\include\afx.h(553) : see declaration of 'CObject::CObject'

    d:\program files\microsoft visual studio 8\vc\atlmfc\include\afx.h(524) : see declaration of 'CObject'

    This diagnostic occurred in the compiler generated function 'CException::CException(const CException &)'

    I only can walk around it by using new:

    Code Snippet

    throw new CMyException(1);

    But I have to delete pointer which is not easy due to this is used through out my application.

    Thanks for all your help!


  • Keith Kiedrowski

    Hi,

    Take a look on a post on my blog http://blogs.msdn.com/nikolad/archive/2005/06/15/429652.aspx
    that explains the similar problem for operator= (line 540 in afx.h). Basically your code starts using private method of CObject because of some compiler generated code. In case of operator= compiler generates this operators on assignment statement aObj = bObj; In your case it is  CObject* paObj = new CObject(CObject); Take a look on a cpp file before this error message and look for exception handling code or other object initialization routines.

    Thanks,
    Nikola Dudar
    Visual C++ Team

  • jerryload

    well here is the code causing the error :

    throw TThreadKillException(); (actually CUSERException subclass ) 



    This compiles in .net 2003 which also has the CObject copy constructor as private but warns with /w4 and fails compile in .net 2005 .


    Also begging the question is why can't the compiler diagnostics tag this particular line in the error messages  That would go a long way in helping to zero in on where the "error" is .   


    the .cpp file

    #include "ThreadKillException.h"

    IMPLEMENT_DYNAMIC(TThreadKillException, CUserException)

    TThreadKillException::TThreadKillException()

    : CUserException()

    {

    }

     

    // The following copy constructor is a bit strange looking. Notice how

    // the base class' default constructor is being called instead of a copy

    // constructor. This is because CUserException does not provide a copy

    // constructor implementation. In order to support rethrowing of

    // exceptions, a copy constructor is needed. Ergo, this copy constructor

    // merely creates a new CUserException to circumvent the lack of a copy

    // constructor.

    TThreadKillException::TThreadKillException(const TThreadKillException& excp)

    : CUserException()

    {

    }

     

    TThreadKillException::~TThreadKillException()

    {

    }

    the .h file

    pragma once

     

    class CLASS_DECLSPEC TThreadKillException : public CUserException

    {

    DECLARE_DYNAMIC(TThreadKillException)

    public:

    TThreadKillException();

    // Implementation

    public:

    TThreadKillException(const TThreadKillException& excp);

    virtual ~TThreadKillException();

    };


  • mc-hammer

    Johnathan-

    I'm struggling with a C2248 error encountered on translating a VS C++ 6 program to VS 2005.  I cannot identify from where in my code it is generated, as it appears as a problem with fstream templates.  Any idea on how to approach this   Here is the compiler error:

    c:\program files\microsoft visual studio 8\vc\include\fstream(802) : error C2248: 'std::basic_ios<Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

    c:\program files\microsoft visual studio 8\vc\include\ios(151):  see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    This diagnostic occurred in the compiler generated function 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const std::basic_ofstream<_Elem,_Traits> &)'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]

  • cmbaker82

    The throw of an instance of a class object causes a copy to be generated when the object is copied into the C++ runtime memory location that holds the exception object.

    It may be possible for some compilers to "elide" (get rid of) this call to the copy-constructor in a similar way to which they can get rid of the call to a copy-constructor when returning an instance of a class object from a function (the RVO optimization) - but the compiler still needs to check that the copy-constructor is callable: so if the copy-constructor is private it should still cause an error even though the compiler would not actually generate a call to it.

    If you throw a pointer to an object, as in your code, then you need to catch a pointer. For example:

    class X { };

    void f()
    {
       throw new X();
    }

    void g()
    {
       try {
          f();
       }
       catch (X* pX) {
          delete pX;
       }
    }


    Note: as object to be thrown is dynamically allocated you need to delete it otherwise you will have a memory leak.

  • Motty

    Hi Jonathan,

    I tracked it down to a derived class that didn't have a public default operator=.  I added one and then it compiled.

    Thanks
    Ted.


  • yet another c2248 error