FIX: 'using namespace std' Before Friend Operator Fails Compile
ID: Q192539
|
The information in this article applies to:
-
Microsoft Visual C++, 32-bit Editions, version 6.0
SYMPTOMS
When a "using namespace std" directive is placed before a friend operator
declaration, you may get one of the following two errors:
Case 1
error C2248: [member variable name] : cannot access private member
declared in class [class name] [file name] (line number) : see
declaration of [member variable]
Case 2
[file name](line number) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++ Help menu, or open the Technical Support help file for more information.
Error executing cl.exe.
CAUSE
The friend operator resolution fails in the compiler front-end when std
namespace is introduced.
RESOLUTION
A supported fix that corrects this problem is now available from Microsoft, but
it has not been fully regression tested and should be applied only to systems
experiencing this specific problem. If you are not severely affected by this
specific problem, Microsoft recommends that you wait for the next Service Pack 3 for Visual Studio 6.0
that contains this fix.
To resolve this problem immediately, contact Microsoft Product Support Services
to obtain the fix. For a complete list of Microsoft Product Support Services
phone numbers and information on support costs, please go to the following
address on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
The English version of this fix should have the following file attributes or later:
Date Time Size File Name Version# Platform
-----------------------------------------------------------------
9/15/98 08:26p 1,183,795 C1xx.dll 12.00.8257.0 (x86)
Workaround
Do one of the following to work around this problem:
- Move the "using namespace std" directive after the friend operator
declaration.
- Avoid using the "using namespace std" directive.
- Forward declare the operator and/or class.
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 Studio 6.0 Service Pack 3.
For more information about Visual Studio service packs, please see the following articles in the Microsoft Knowledge Base:
Q194022 INFO: Visual Studio 6.0 Service Packs, What, Where, Why
Q194295 HOWTO: Tell That Visual Studio 6.0 Service Packs Are Installed
MORE INFORMATION
Test1.cpp Reproduces the C2248 Error in Case 1
// test1.cpp
// Compiler options needed: cl /c /GX
#include <iostream>
using namespace std;
#ifdef WORKAROUND
class CMyClass;
ostream& operator <<(ostream &, const CMyClass &);
#endif // WORKAROUND
class CMyClass
{
public:
friend ostream& operator <<(ostream &, const CMyClass &);
// An alternative workaround is to define the operator in the
// class declaration. In this case it is not necessary to forward
// declare the class and friend function.
private:
int value;
};
ostream& operator <<(ostream& out, const CMyClass& m)
{
int i = m.value; // C2248 Error
return out;
}
void main()
{ }
Test2.cpp Reproduces the C1001 Error in Case 2
// test2.cpp
// Compiler options needed: cl /c /GX
#include <iostream>
using namespace std;
#ifdef WORKAROUND
int operator+(int, const class &);
#endif // WORKAROUND
class CMyClass
{
public:
friend int operator+(int, const CMyClass &); // C1001 Error.
};
void main()
{ }
Keywords : kberrmsg kbservicepack kbLangCPP kbVC600bug kbVS600sp2 kbVS600SP1 kbVS600sp3fix
Version : winnt:6.0
Platform : winnt
Issue type : kbbug