Index: tol/contrib/ANN/tolANN.cpp
===================================================================
--- tol/contrib/ANN/tolANN.cpp	(revision 2113aeb5edcd79f4bc244a0ce966dfaa29338e5a)
+++ tol/contrib/ANN/tolANN.cpp	(revision c9fda30cd1de3634b7392ba2e6d4e5089a0abde3)
@@ -64,4 +64,5 @@
   
   int get_dim( ) { return tree->theDim( ); }
+  int get_size( ) { return size; }
   
   static double code_addr( tol_kdtree* ptr )
@@ -184,11 +185,13 @@
   if ( dK.IsUnknown( ) || 
       (dK.Value() <= 0) || 
-      (dK.Value() > qpoints.Rows()) ) {
+      (dK.Value() > tree->get_size()) ) {
     Error( I2( "Invalid k argument: must be positive and lesser or equal than "
                "number of reference points.",
                "Argumento k es invalido: debe ser positivo y menor o igual que "
-               "el número de puntos de referencia." ) );
+               "el número de puntos de referencia." )+"\n"+
+           "(0<(k="+dK+")<="+tree->get_size()+")==False" );
     return;
   }
+
   int k = int( round( dK.Value( ) ) );
   ANNpoint pt = annAllocPt( tree->get_dim(), 0.0 );
