Friday, July 11, 2008

Fixing "ambiguous symbol" in a C++/CLR project

I was trying to use a CString in a managed project today. This is supposed to be easy but it wasn't working. [Note: Microsoft factored CString from MFC several years ago, so CString can now be used standalone. Super handy class.]

I started with what I thought was a reasonable statement:

#include "cstringt.h"

This gave me numerous errors, starting with:

C:\Program Files\Microsoft SDKs\Windows\v6.0\Include\ocidl.h(6238) : error C2872: 'IServiceProvider' : ambiguous symbol
C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\atlcomcli.h(370) : error C2872: 'DISPPARAMS' : ambiguous symbol


With a little research, I determined I was using the wrong include file. So I switched to:

#include "atlstr.h"

When I tried to do that, I received similar errors:

C:\Program Files\Microsoft SDKs\Windows\v6.0\Include\ocidl.h(6238) : error C2872: 'IServiceProvider' : ambiguous symbol
C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\atlcomcli.h(370) : error C2872: 'DISPPARAMS' : ambiguous symbol

Several other posts inquiring about the same errors received no answers.

The problem is caused because the Microsoft SDK (aka Platform SDK) and mscorlib both have definitions for these classes.

The solution is to take the #include file that caused the problem and make sure it is placed before any "using namespace" statement. In my case, that meant moving #include near the top of stdafx.h.

12 comments:

  1. Finally one quick solution after lot of googling. Thank you very much.

    ReplyDelete
  2. Thank you so much....it was a HUGE help!!

    ReplyDelete
  3. Awesome, but I still don't fully understand why it works.

    ReplyDelete
  4. Hi Jim
    I am facing similar problem but unable to fis it.

    I am making a project and attaching a library code in it. Both projects are made with same project setting . In library project i have header file where i typedef CString but when i am compiling the main project I am getting following errors

    C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxwin.h(3343): error C2872: 'CString' : ambiguous symbol

    I tried changing the order of stdafx.h and other header files but no success.
    Please help me out.

    ReplyDelete
  5. I'm not sure what you mean when you say "I typedef CString", but you can't typedef CString. You can only use the definitions that appear in the ATL header files because it's really a template underneath the covers. In any case, if CString were in your precompiled headers, there should be no need for you to typedef it.

    ReplyDelete
  6. Thaks a lot!!!
    Problem of ambigous symbol, while using native C++ from CLR C++ solved!

    ReplyDelete
  7. Thanks!!! It's 2015, and I still ran into the same problem!

    ReplyDelete
  8. Thanks a lot!!!

    ReplyDelete
  9. Thank you so much. appreciate it !

    ReplyDelete