C++,跨不同 namespace 的名称冲突

标签 c++ namespaces

我对以下名称冲突感到困惑:

namespace mp2 {

boost::numeric::ublas::matrix_range<M>
slice(M& m, const R1& r1, const R2& r2) {
    namespace ublas = boost::numeric::ublas;
    ublas::range r1_(r1.begin(), r1.end()), r2_(r2.begin(), r2.end());
    return ublas::matrix_range<M>(m, r1_, r2_);
}

double energy(const Wavefunction &wf) {
    const Wavefunction::matrix& C = wf.coefficients();
    int No = wf.occupied().size();

    foreach (const Basis::MappedShell& P, basis.shells()) {
        slice(C, range(No), range(P));

g++4.4 的错误是

 7 In file included from mp2.cpp:1:
 8 /usr/include/boost/numeric/ublas/fwd.hpp: In function âdouble mp2::energy(const Wavefunction&)â:
 9 /usr/include/boost/numeric/ublas/fwd.hpp:32: error: âboost::numeric::ublas::sliceâ is not a function,
10 ../../src/mp2/energy.hpp:98: error:   conflict with âtemplate<class M, class R1, class R2> boost::numeric::ublas::matrix_range<M> mp2::slice(M&, const R1&, const R2&)â
11 ../../src/mp2/energy.hpp:123: error:   in call to âsliceâ
12 /usr/include/boost/numeric/ublas/fwd.hpp:32: error: âboost::numeric::ublas::sliceâ is not a function,
13 ../../src/mp2/energy.hpp:98: error:   conflict with âtemplate<class M, class R1, class R2> boost::numeric::ublas::matrix_range<M> mp2::slice(M&, const R1&, const R2&)â
14 ../../src/mp2/energy.hpp:129: error:   in call to âsliceâ
15 make: *** [mp2.lo] Error 1

ublas段是

namespace boost { namespace numeric { namespace ublas {
    typedef basic_slice<> slice;

为什么 ublas 中的 slice 与 mp2 中的 slice 发生冲突? 我非常确定代码和包含中没有 using namespace ublas

谢谢

最佳答案

我认为有 argument-dependent lookup继续。

slice(C, range(No), range(P));

两个 range 参数似乎来自 boost::numeric::ublas(可能是用 using boost::numeric::ublas::range;),所以编译器认为名称来自这个命名空间,其中包括 slice 类型。

关于C++,跨不同 namespace 的名称冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3027697/

相关文章:

c++ - 如何在字符串 vector 中引用字符串的特定字符?

java - 如何拦截安卓设备上的音频流?

C#:如何将网站项目转换为 Web 项目

php - 如何为个人开发者选择命名空间?

c# - 继承是否也会自动从其父级继承 "inherit"命名空间?

C++ 函数调用路由解析器

c++ - 与另一个类 C++ 共享对象

c++ - 斐波那契函数的问题。 C++

xml - xpath-默认命名空间 : matching nodes from loaded XML without namespace

c++ - 通过 if 条件切换命名空间