FIX: Error C2245 Generated Incorrectly
PSS ID Number: Q108899
Article last modified on 03-15-1994

7.00

MS-DOS


----------------------------------------------------------------------
The information in this article applies to:

 - Microsoft C/C++ compiler for MS-DOS, version 7.0
----------------------------------------------------------------------

SYMPTOMS
========

When the sample code below is compiled using C/C++ 7.0, the following
error is generated:

  error C2244: nonexistent function 'f' specified as friend

CAUSE
=====

The problem occurs when there is a mismatch between a forward declaration
for a class or a struct type and the subsequent declaration/definition for
the type. For example, in the sample code below, there is a forward
declaration of type A as a struct. Then A is declared as a class. This
causes the C2244 error to occur on the friend function in class A.
Reversing the example so that the forward declaration is for a class and
the subsequent declaration is for a struct also produces the error.

RESOLUTION
==========

The error can be eliminated by consistently declaring the type as either a
class or a struct. In other words, the data type used in the forward
declaration should match the data type of the subsequent
declaration/definition.

STATUS
======

Microsoft has confirmed this to be a problem with the Microsoft C/C++
compiler version 7.0. This problem is fixed in the C/C++ compiler version
8.0, which ships with Visual C++ for Windows, version 1.0.

MORE INFORMATION
================

The problem arises because of an inconsistent use of struct and class for
the same name. The class declaration is flagged by the compiler with the
following level 2 warning:

   warning C4099: type declared with 'struct' is defined with 'class'

Consistent use of either class or struct with the name eliminates both the
warning and the error.

Sample Code
-----------

/* Compile options needed: /c /W2
*/

struct A;

struct B {
    int f(A&) {return 1;}
};

class A { // Works fine if class is changed to struct
public:
    friend int B::f(A&);
};

Additional reference words: 7.00 8.00 C4099
KBCategory: Tls
KBSubcategory: CPPLngIss

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

Copyright Microsoft Corporation 1994.
