FIX: C2243 on Friend Overload o

Last reviewed: September 19, 1997
Article ID: Q148688
The information in this article applies to:
  • The Microsoft C/C++ Compiler (CL.EXE) included with: - Microsoft Visual C++, 32-bit Edition, versions 4.0, 4.1, 4.2

SYMPTOMS

The following error is generated:

   error C2243: 'abstract declarator' : conversion from 'class Derived *'
   to 'const class Base<Type> &' exists, but is inaccessible

in code that contains all of the following:
  • A template base class that has a friend function that overloads operator << for ostream.
  • A derived class that privately inherits its base class and has a friend function that overloads operator << for ostream where the second argument to the operator << is a reference to const type (const Derived &).
  • A call to the operator << passing a non-const derived type object.

RESOLUTION

Use one of the following two workarounds to resolve the problem:

  • Typecast the derived object to a reference to const type when using the operator <<.

    -or-

  • Make the base class public instead of private.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++ version 5.0.

MORE INFORMATION

Sample Code

   /* Compile options needed: None
   */

   #include <iostream.h>

   template<class T>
   class Base;

   template<class T>
   ostream& operator<<(ostream& s, const Base<T> & a)
   {
      return s;
   }

   template<class T>
   class Base
   {
   public:
      friend ostream& operator<<(ostream& s, const Base<T>& a);
   };

   class Derived;

   ostream& operator<<(ostream& s, const Derived& a)
   {
      return s;
   }

   // Replace 'private' by 'public' in the following line
   // to avoid the error (work around # 2)
   class Derived : private Base<char> {
   public:
      friend ostream& operator<<(ostream& s, const Derived& a);
   };

   void main(void)
   {
      Derived objDerived;

      // The C2243 error is generated for the following line.
      // Uncomment the typecast to avoid the error (work around # 1)
      cout << /*(const Derived&)*/ objDerived;
   }
Keywords          : CPPIss CPPLngIss vcbuglist400 vcfixlist500 kbprg
Version           : 4.0 4.1 4.2
Platform          : NT WINDOWS
Issue type        : kbbug
Solution Type     : kbfix


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 19, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.