0

what is the meaning of the :: , without preceding by anything

::flann::SearchParams param_k_;

I get below errors on one project but not on another.

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

Could anyone please help me with understanding whats the use of unanimous :: , and how to resolve the issue?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

2 Answers2

5

According to the C++ Standard (6.3.6 Namespace scope)

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.

And the global namespace does not have a name.

So this record

::flann::SearchParams param_k_;

denotes that the name flann should be searched in the global namespace or in one of its inline namespaces. Or if the name was not found then all namespaces nominated in using directives implicitly (as for an unnamed namespace) or explicitly (recursively) in the global namespace are searched.

Here is a demonstrative program

#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;
}

Its output is

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

As the inline namespace of the global namespace has its own name then the last statement can be also rewritten like

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

Or even like

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

Below there is a more complicated example

#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;
}

There is no ambiguity relative to the qualified name lookup of the name ::y because the first set of namespaces that are searched is the nominated namespace (in this case the global namespace) and its inline namespaces. And the name y is found in this set of namespaces. Otherwise the compiler would continue to search the name y also in unnamed namespaces of the global namespace.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
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)
}
}

The error is complaining that flann::SearchParams has not been defined. This usually happens when the type has been forward declared, but not defined. Find the header file that defines the SearchParams type, and include it in your code. That should fix the issue.

robthebloke
  • 9,331
  • 9
  • 12