Thread: error C2872: 'CString' : ambiguous symbol

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    7

    error C2872: 'CString' : ambiguous symbol

    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.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, what does line 3343 of afxwin.h say?

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You have CString 'defined' twice.

    Usually this comes from the object (CString) being defined in an 'include' and a 'using'.

    Try removing/re-ordering your includes/usings until the error goes away.

    Sometimes this will require a 'forward declaration' of an object (declare the object as a stub, which is later over-written by a full decalration).

    Quote Originally Posted by CommonTater View Post
    Well, what does line 3343 of afxwin.h say?
    The first declaration of a method containing a CString param.
    Last edited by novacain; 09-16-2010 at 07:46 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    7
    Thanks for ur reply.
    But i didn't get from line :
    Sometimes this will require a 'forward declaration' of an object (declare the object as a stub, which is later over-written by a full decalration).
    what do u want to say by that

    I am trying to reorder the includes and using but getting no success

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try removing all includes and usings.
    Adding them back only to remove errors (checking that they are needed.)

    Check you have not '#define CString' anywhere.

    Are you using conditional compilation where required?

    Code:
    #ifndef _MY_DECLARES_INCLUDED_ // if we have not already defined this name then 
    #define _MY_DECLARES_INCLUDED_ //define this name to stop this being included twice
    
    //defines, structs, usings etc
    
    #endif
    Did you build the lib and app using the same version of the MFC DLLs?

    Did you edit the stdafx.h?

    Are you putting all global includes and usings in the stdafx.h? (anything the whole app or every class requires) [and inside the stdafx.h's conditional compilation loop]



    A 'forward declaration' is probably not required in this case (as it is not one of your objects) I mentioned it to cover all the options.
    Last edited by novacain; 09-18-2010 at 12:09 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    7

    error C2872: 'CString' : ambiguous symbol

    Hi
    Thanks for your reply
    Answers to your questions are:

    I tried to reorder header files but no success.
    If i remove certain header files then i am getting lot of undeclared errors.
    Somewhere in library code i have typedef CString
    Yes I am using conditional compilation where ever required.
    I am building lib and app with same version & project settings

    here is my stdafx.h:
    Code:
    #pragma once
    
    #ifndef _SECURE_ATL
    #define _SECURE_ATL 1
    #endif
    
    #ifndef VC_EXTRALEAN
    #define VC_EXTRALEAN            // Exclude rarely-used stuff from Windows headers
    #endif
    
    #include "targetver.h"
    
    #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // some CString constructors will be explicit
    
    extern "C"
    {
    #include <..my header files>
    #include <..my header files>
    }
    
    // turns off MFC's hiding of some common and often safely ignored warning messages
    #define _AFX_ALL_WARNINGS
    
    #ifndef _AFXDLL
    #define _AFXDLL
    #endif
    
    
    #include <afxwin.h>         // MFC core and standard components
    #include <afxext.h>         // MFC extensions
    
    #ifndef _AFX_NO_OLE_SUPPORT
    #include <afxdtctl.h>           // MFC support for Internet Explorer 4 Common Controls
    #endif
    #ifndef _AFX_NO_AFXCMN_SUPPORT
    #include <afxcmn.h>                     // MFC support for Windows Common Controls
    #endif // _AFX_NO_AFXCMN_SUPPORT
    
    #ifdef _UNICODE
    #if defined _M_IX86
    #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
    #elif defined _M_IA64
    #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
    #elif defined _M_X64
    #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
    #else
    #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
    #endif
    #endif
    To give u more clarity here is the cpp file where i am getting error
    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 ...;              
    using namespace ...;
    using namespace ...;
    namespace ...;

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Is it one of your files that defines CString or a system one?

    You should not include windows.h in an MFC app (as your stdafx.h includes afxwin.h).

    Is WIN32 defined?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    7
    I have defined CString (typedef CObjectSimple<pString> CString in header file A which is included in header file B. I have included header file B in my app ( code is given above)

    I commented the win32 include but got no success.
    Well win32 is already defined in preprocesser settings.

    error is given by system file as u can see
    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.
    Last edited by Ravikant; 09-21-2010 at 12:10 AM. Reason: forget to remember important information

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by Ravikant View Post
    one thing which i forget to tell is that this code is working fine in Debug mode. Release mode is giving problem.
    Check the libs you are linking to in release mode and app settings (compared to debug mode).

    Debug versions of the libs usually have a 'd' at the end.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    7
    I already checked this. Output name is different in both configuration.

    One more thing to state that may help you to find problem is that if I include afx.h instead of stdafx in my file then it compiles fine but when i compile my project then some linking errors comes. That means to include this file i have to include stdafx in that file.
    now can u guess anything?

    can u pls help me how compiler behaves while including header files and namespace. As far as i think compiler start compiling from top of file so stdafx compiles first but in this case why it conflicts with the definition of a file which is still not included.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Compiler parses from top to bottom.
    At this point, you just have to disable headers one after one (in stdafx.h) until the problem disappears.
    Comment out your code, as well, so it does not cause compile errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by Ravikant View Post
    myheaderFilelocation(295) : myclassobject::CString'
    Can you post this code (and the lines around it)?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM