开发者

C++/CLI - Ambiguous symbol of a namespace and a class

开发者 https://www.devze.com 2023-02-20 10:03 出处:网络
I try to compile a C++/CLI file, that is including a header file (native, third-party) wherein a class \"Fo开发者_Go百科o\" is defined. Furthermore I am using a C# dll via #using \"Bar.dll\" wherein a

I try to compile a C++/CLI file, that is including a header file (native, third-party) wherein a class "Fo开发者_Go百科o" is defined. Furthermore I am using a C# dll via #using "Bar.dll" wherein a namespace "Foo" is defined. The Compiler gives the Error C2872 "Foo is ambiguous symbol".

I dont know in which namespace the native class "Foo" is defined as the class defintion in the header file is not nested in a certain namespace. So I assume the class "Foo" might be in no namespace (that is possible in C++, isnt it?). Otherwise I would specify the class "Foo" with its namespace to make it specific for the compiler.

I have the possibility to rename the namespace "Foo" used in "Bar.dll" but I am looking for a different solution to keep the namespace. Actually a class and a namespace are different elements - but not for the c++/cli compiler I guess?

Thx in advance Michbeck


I'm not sure if this will work, but it's worth a try.

namespace FooNamespace = Foo;
typedef Foo FooClass;

The namespace directive is only valid on namespaces, not classes. Vise-versa with the typedef. You should be able to use FooClass and FooNamespace in your code.


I've run into a similar problem with a huge proprietary codebase that would be too expensive to change. The issue seems to pop up for me when I try the following:

using namespace System;

The code I'm including happens to have their own Enum class, and if you start using the "System" namespace, then the two names clash. To solve this, I simply put this inside of the namespace { } heading.

namespace MyNamespace
{
    using namespace System;

    ...
}

This might not work for the #using directive, but it might be worth a try.

#using "Bar.dll"

namespace MyNamespace
{

    using namespace System;
    using namespace Foo;

    ...
}


In addition to a typedef for the class "Foo" (Courtesy David Yaw) the problem can be solved by using the global namespace for the class "Foo": ::Foo . So the compiler can distinguish between this class and the namespace "Foo" in the "Bar.dll"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号