Search code examples
c++forward-declarationkdtree

Forward declaration in ANN Library


I'm using the ANN Library (https://www.cs.umd.edu/~mount/ANN/). There is a function

ANNkdTree::getStats(ANNkdStats &st)

which provides the kdtree statistics. The library's manual defines this function like this:

class ANNkdStats { // stats on kd-tree
public:
   int dim; // dimension of space
   int n_pts; // number of points
   [...]
}

However, if I follow the function calls, I can only find a forward declaration

class ANNkdStats;

The only thing that I want to do is a simple use of this function

ANNkdStats st;
kdTree->getStats(st);

And the compiler output follows:

37: error: invalid use of incomplete type ‘class ANNkdStats’
     ANNkdStats *st = new ANNkdStats();

include/ANN/ANN.h:701:7: error: forward declaration of ‘class ANNkdStats’
 class ANNkdStats;    // stats on kd-tree

I'm not used to work with forward declaration methods, and I don't know how to solve it, because I can't modify the library.

Thanks in advance for your answers. :D


Solution

  • ANNkdStats class is defined inside the ANN/ANNperf.h header file:

    class ANNkdStats {          // stats on kd-tree
    public:
        int     dim;            // dimension of space
        int     n_pts;          // no. of points
    
        // ...
    
        ANNkdStats()            // basic constructor
        { reset(); }
    
        void merge(const ANNkdStats &st);   // merge stats from child 
    };
    

    Adding a

    #include "ANN/ANNperf.h"
    

    directive should be enough (ANNperf.h includes in turn ANN.h).

    This assumes that the ANN include directory is already on the compiler's search path (g++ -Iinclude_dir).