c++ -::(范围解析运算符) 前面没有任何内容

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

这个问题在这里已经有了答案:





C++ : what is :: for?

(5 个回答)


去年关闭。




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

::flann::SearchParams param_k_;

我在一个项目上遇到以下错误,但在另一个项目上没有。
error C2079: 'pcl::KdTreeFLANN<pcl::PointXYZ,flann::L2_Simple<float>>::param_radius_' uses undefined struct 'flann::SearchParams'

任何人都可以帮助我了解一致::的用途以及如何解决问题?

最佳答案

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

2 A namespace member can also be referred to after the :: scope resolution operator (8.1) applied to the name of its namespace or the name of a namespace which nominates the member’s namespace in a using-directive; see 6.4.3.2.



并且全局命名空间没有名称。

所以这个记录
::flann::SearchParams param_k_;

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

这是一个演示程序
#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也在全局命名空间的未命名命名空间中。

关于c++ -::(范围解析运算符) 前面没有任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59781645/

相关文章:

opencv - 查找图像中连续的黑色像素

c++ - operator << argument-dependent lookup 不在全局命名空间中查找

c++ - 命名空间中的未命名空间

visual-studio - 为什么 Visual Studio 2010 无法找到/打开 PDB 文件?

C# - 将文本文件作为类加载

c++ - 指针差异

c++ - 为什么必须在每个使用模板的函数之前声明模板?

c++ - 动态分配的成员变量。重点是什么?

c++ - 我可以在 C++ 项目中混合使用 c 和 cpp 文件吗?

c++ - findChessboardCorners 的 OpenCV 问题