Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 解决命名空间冲突_C++_Namespaces - Fatal编程技术网

C++ 解决命名空间冲突

C++ 解决命名空间冲突,c++,namespaces,C++,Namespaces,我有一个包含大量符号的名称空间,但我想覆盖其中一个: 外部图书馆 namespace LottaStuff { class LotsOfClasses {}; class OneMoreClass {}; }; 我的文件.h using namespace LottaStuff; namespace MyCustomizations { class OneMoreClass {}; }; using MyCustomizations::OneMoreClass; my_file.cpp in

我有一个包含大量符号的名称空间,但我想覆盖其中一个:

外部图书馆

namespace LottaStuff
{
class LotsOfClasses {};
class OneMoreClass {};
};
我的文件.h

using namespace LottaStuff;
namespace MyCustomizations
{
class OneMoreClass {};
};
using MyCustomizations::OneMoreClass;
my_file.cpp

int main()
{
    OneMoreClass foo; // error: reference to 'OneMoreClass' is ambiguous
    return 0;
}
如何在不使用一千个单独的“using xxx;”语句替换“using namespace LottaStuff”的情况下解决“dimensional”错误


编辑:也就是说,我不能编辑我的\u file.cpp,只能编辑我的\u file.h。因此,用MyCustomizations::OneMoreClass everywhere替换OneMoreClass是不可能的。

您应该明确指定您想要的
OneMoreClass

int main()
{
    myCustomizations::OneMoreClass foo;
}

当您说“
usingnamespace
”时,名称空间的整个要点就失败了

所以把它拿出来,使用名称空间。如果需要using指令,请将其放在main中:

int main()
{
    using myCustomizations::OneMoreClass;

    // OneMoreClass unambiguously refers
    // to the myCustomizations variant
}
了解
使用
指令的作用。你所拥有的基本上是:

namespace foo
{
    struct baz{};
}

namespace bar
{
    struct baz{};
}

using namespace foo; // take *everything* in foo and make it usable in this scope
using bar::baz; // take baz from bar and make it usable in this scope

int main()
{
    baz x; // no baz in this scope, check global... oh crap!
}
一个或另一个将起作用,同时将一个放置在
main
的范围内。如果您发现名称空间的键入非常繁琐,请创建一个别名:

namespace ez = manthisisacrappilynamednamespace;

ez::...

但永远不要在头中使用
名称空间,也可能永远不要在全局范围中使用。在本地范围内也可以。

但是如果我不能编辑我的_file.cpp怎么办?编辑了我上面的问题。@Kyle:然后停止使用
名称空间
。您不能使用它然后抱怨名称空间冲突。这就像你的车里有安全带,但是你撕掉了它们,然后抱怨你不安全。这里有一个提示-不要使用“using”关键字,而是引用具有完全限定名称的类:vector->:::std::vector!C++11将为名称空间(内联名称空间)的版本控制提供一个很好的功能,它将允许用户准确地获得这个。。。同样,您可能需要等待更多的时间才能在编译器中使用该功能(除非您使用的是gcc>4.4),请参见