【问题标题】:::(scope resolution operator) preceded by nothing [duplicate]::(范围解析运算符)前面没有任何内容[重复]
【发布时间】:2020-01-17 05:22:04
【问题描述】:

:: 是什么意思,前面没有任何东西

::flann::SearchParams param_k_;

我在一个项目上遇到以下错误,但在另一个项目上却没有。

error C2079: 'pcl::KdTreeFLANN<pcl::PointXYZ,flann::L2_Simple<float>>::param_radius_' uses undefined struct 'flann::SearchParams'

谁能帮助我理解一致 :: 的用途,以及如何解决这个问题?

【问题讨论】:

    标签: c++ opencv namespaces point-cloud-library name-lookup


    【解决方案1】:

    根据 C++ 标准(6.3.6 命名空间范围)

    2 命名空间成员也可以在 :: 范围之后引用 解析运算符 (8.1) 应用于其命名空间的名称或 命名空间的名称,它在 a 中指定成员的命名空间 使用指令;见 6.4.3.2。

    而且全局命名空间没有名字。

    所以这条记录

    ::flann::SearchParams param_k_;
    

    表示名称flann 应在全局命名空间或其内联命名空间之一中进行搜索。或者,如果未找到名称,则搜索在全局命名空间中隐式(对于未命名的命名空间)或显式(递归)使用指令指定的所有命名空间。

    这是一个演示程序

    #include <iostream>
    
    int x = 1;
    
    inline namespace N1
    {
        int y = 2;
    }
    
    int main() 
    {
        int x = 10;
        int y = 20;
    
        std::cout << "x + ::x = " << x + ::x << '\n';
        std::cout << "y + ::y = " << y + ::y << '\n';
    
        return 0;
    }
    

    它的输出是

    x + ::x = 11
    y + ::y = 22
    

    由于全局命名空间的内联命名空间有自己的名字,所以最后一条语句也可以改写为

    std::cout << "y + N1::y = " << y + N1::y << '\n';
    

    甚至喜欢

    std::cout << "y + ::N1::y = " << y + ::N1::y << '\n';
    

    下面有一个更复杂的例子

    #include <iostream>
    
    int x = 1;
    
    inline namespace N1 
    {
        int y = 2;
    }
    
    namespace 
    {
        int y = 3;
    }
    
    int main() 
    {
        int x = 10;
        int y = 20;
    
        std::cout << "x + ::x = " << x + ::x << '\n';
        std::cout << "y + ::y = " << y + ::y << '\n';
    
        return 0;
    }
    

    对于名称::y 的限定名称查找没有歧义,因为搜索的第一组命名空间是指定的命名空间(在本例中为全局命名空间)及其内联命名空间。在这组命名空间中可以找到名称 y。否则编译器将继续在全局命名空间的未命名命名空间中搜索名称 y

    【讨论】:

      【解决方案2】:
      int i; //< global
      
      namespace foo
      {
      int i = 0; //< declaration hides global i
      
      void method()
      {
        i = 42;  //< sets the value of foo::i
      
        ::i = 43; //< sets the value of i in the parent namespace (global i)
      }
      }
      

      错误是抱怨flann::SearchParams 尚未定义。这通常发生在类型已被前向声明但未定义时。找到定义 SearchParams 类型的头文件,并将其包含在您的代码中。那应该可以解决问题。

      【讨论】:

        猜你喜欢
        • 2010-09-09
        • 2012-04-20
        • 1970-01-01
        • 1970-01-01
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        • 2014-10-05
        • 2015-12-18
        相关资源
        最近更新 更多