Вопрос

I try to compile a C++/CLI file, that is including a header file (native, third-party) wherein a class "Foo" 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"

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top