Microsoft KB Archive/148688

= FIX: C2243 on Friend Overload o =

Article ID: 148688

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition

-



This article was previously published under Q148688



SYMPTOMS
The following error is generated:

  error C2243: 'abstract declarator' : conversion from 'class Derived *' to 'const class Base &' 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.



Sample Code
/* Compile options needed: None */   #include    template   class Base; template  ostream& operator<<(ostream& s, const Base & a)   { return s;  } template  class Base {  public: friend ostream& operator<<(ostream& s, const Base& 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 { 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; }

Additional query words: kbVC400bug

Keywords: kbbug kbfix kblangcpp kbvc500fix kbcpponly kbcompiler KB148688

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.