CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: using namespace

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,592

    using namespace

    Several files contain objects called Rectangle. For example it could be this version, from wingdi.h:-

    Code:
    WINGDIAPI BOOL     WINAPI Rectangle(HDC,int,int,int,int);
    or this version which is a c'tor in a library called gdkmm:-

    Code:
    namespace Gdk
    {
    
    class Rectangle
    {
    	public:
    		Rectangle();
    
    	// whatever...
    };
    }
    and the source code I'm building says this:-

    Code:
    using namespace Gdk;
    
    Rectangle monitor;
    but when I try to compile this with VS2005 it gives me this error:-

    error C2872: 'Rectangle' : ambiguous symbol
    could be 'C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\wingdi.h(3514) : BOOL Rectangle(HDC,int,int,int,int)'
    or 'F:\+GTK-SOURCES\gnu-windows\include\gdkmm/rectangle.h(42) : Gdk::Rectangle'
    Obviously it's easy to fix - but why do I even get the error?? I've told it to use namespace Gdk - but even if I hadn't done that, the gdk version is the only one with a matching function signature. So why is the compiler even confused about this
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: using namespace

    Quote Originally Posted by John E View Post
    Obviously it's easy to fix - but why do I even get the error?? I've told it to use namespace Gdk - but even if I hadn't done that, the gdk version is the only one with a matching function signature. So why is the compiler even confused about this
    Gdk::Rectangle is not a function name ( it's a type name ) hence no overload resolution is possible here; the two names are looked up and an ambiguity arises about who's who, that's it.

    Indeed, an alternative solution to qualifying the name could be using an elaborated type specifier ( forcing the compiler to explicitly look up types only ), as in the C days:

    Code:
    using namespace Gdk;
    
    class Rectangle monitor;
    Last edited by superbonzo; August 18th, 2016 at 10:00 AM. Reason: typos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)