locked
error C2872: 'CString' : ambiguous symbol

    Question

  • I am building a project in which 1 other project is also included. My both projects are built with "Use MFC in a Static Library" and "MT" options. My second project is a library which is compiled successfully but when i compile my main project i get following errors:
    C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxwin.h(3343): error C2872: 'CString' : ambiguous symbol

    Can anybody tell me the real cause of this error


    I am trying to reorder the includes and using namespaces but getting no success.
    Somewhere in library code i have defined CString (typedef CObjectSimple CString in header file A which is included in header file B. I have included header file B in my app

    Collapse | Copy Code
    #pragma once
    #include "stdafx.h" #include "PDFTools.h"
    #include <sstream>
    #include <myheaderfiles>
    #include <myheaderfiles> //This is header file which is giving error and which include the header file where CString is typedef.
    #include <myheaderfiles>
    #include <myheaderfiles>
    #include <myheaderfiles>
    #include <myheaderfiles>
    #include <myheaderfiles>
    #include <boost/program_options.hpp>
    #include <boost/algorithm/string/replace.hpp>
    #include <limits>
    #include <myheaderfiles>
    #include <myheaderfiles>

    #ifdef WIN32
    #include <Windows.h>
    #undef max
    #undef min
    #endif
    using namespace std;
    using namespace boost;

    using namespace ...; This is namespace which is getting error
    using namespace ...;
    namespace ...;

    I commented the win32 include but got no success.

    Error is given by system file as u can see
    Collapse | Copy Code
    C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxwin.h(3331): error C2872: 'CString' : ambiguous symbol
    could be 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxstr.h(99) : ATL::CStringT<BaseType,StringTraits> CString' with
    [
    BaseType=wchar_t,
    StringTraits=StrTraitMFC_DLL<wchar_t> ]
    or .....myheaderFilelocation(295) : myclassobject::CString' C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxwin.h(3361) : see reference to function template instantiation 'TReturnType CWnd::EnlargeBufferGetText<TReturnType,int>(TRetur nType,LPTSTR &,TCchType &,TCchType,TCchType,UINT,WPARAM &,LPARAM &,CString &) throw(...) const' being compiled
    with
    [
    TReturnType=BOOL,
    TCchType=int
    ]
    C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxwin4.inl(132) : see reference to function template instantiation '
    TReturnType CWnd::EnlargeBufferGetText<BOOL>(TReturnType,LPTST R &,int &,UINT,WPARAM &,LPARAM &,CString &) throw(...) const' being compiled
    with
    [
    TReturnType=BOOL
    ]

    one thing which i forget to tell is that this code is working fine in Debug mode. Release mode is giving problem
    Thursday, October 07, 2010 11:21 AM

All replies

  • On 07/10/2010 13:21, RaviKantD wrote:

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

    Somewhere in library code i have defined CString (typedef CObjectSimple CString in header file

    Would it be possible for you to just use another name for your custom typedef'ed "CString"?
     Giovanni

    Thursday, October 07, 2010 11:37 AM
  • Sorry it would be a large work as that is declared in library project and it is working fine.

    Can u tell me any other way?

    Thursday, October 07, 2010 11:59 AM
  • You can remove the typedef, and you can remove "using namespace" for the offending namespace and use qualified name (mynamespace::CString).

    You are asking that compiler somehow knows, when compiling your sources, that when it reads CString on line X, it should use CString from your namespace, and that, when it sees same word, CString on line Y, it should use CString from MFC.

    Compiler would have to read your mind to know which CString to pick, because it is ultimately you who gave the same name to two things.

    Goran.

    Thursday, October 07, 2010 12:11 PM