Microsoft KB Archive/820648

= A C2871 compiler error occurs when you add a reference to a COM component in managed Visual C++ .NET =

Article ID: 820648

Article Last Modified on 1/6/2006

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition

-





SYMPTOMS
You may use Component Object Model (COM) components from a Managed C++ application by using the Add Reference feature in Visual C++ .NET 2003. The Add Reference feature creates an interop assembly. You may use using namespace to import the types that are contained in a namespace. However, when you try to import the namespace, you may receive the following compiler error message:

error C2871: ' ' : a namespace with this name does not exist



CAUSE
The namespace of the COM component is nested under the Interop namespace. Refer the Interop namespace before any COM component namespace. When you use the Add Reference feature, Visual C++ .NET does not put using namespace Interop; in a source file.



RESOLUTION
To resolve the problem, import the Interop namespace, and then import the namespace of the COM component. For example, if you reference the Microsoft Scripting Runtime library, you can use the following code to resolve the problem: using namespace Interop; using namespace Scripting;



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce the Behavior
 Start Visual Studio .NET 2003. On the File menu, point to New, and then click Project. Under Project Types, expand Visual C++ Projects, and then click .NET. Click Console Application (.NET) under Templates. In the Name text box, type Sample, and then click OK.</li> In Solution Explorer, right-click Reference, and then click Add Reference.</li> In the Add Reference dialog box, click the COM tab.</li> In the Component Name list, click Microsoft Scripting Runtime, and then click OK.</li>  Open the Sample.cpp file, and then add the following code by using the System namespace: using namespace Scripting; </li> Press the CTRL + SHIFT + B key combination to build the solution. You may receive the compilation error that the &quot;Symptom&quot; section describes.</li></ol>

Keywords: kberrmsg kbtshoot kbide kbnamespace kbprb kbcominterop KB820648

-

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

© Microsoft Corporation. All rights reserved.