invalid use of incomplete type ‘class vtkPolyData’

Hello everyone,

I would like to test whether an array is present in a polydata, but I am getting this error at compile time:

NicocoTest.cxx:10:39: error: invalid use of incomplete type ‘class vtkPolyData’
   if (reader.GetPointer()->GetOutput()->GetPointData()->HasArray("ARRAYNAME")) {
                                       ^~
In file included from NicocoTest.cxx:1: VTK-8.2.0/IO/Legacy/vtkPolyDataReader.h:37:7: note: forward declaration of ‘class vtkPolyData’
 class vtkPolyData;
       ^~~~~~~~~~~

Here is the source code leading to this hard-to-understand-for-c++-noob error:

#include "vtkPolyDataReader.h"
#include "vtkNew.h"

int main(int argc, char *argv[]) {
  std::cout << "Reading file" << std::endl;
  
  vtkNew<vtkPolyDataReader> reader;
  reader->SetFileName(argv[1]);

  if (reader.GetPointer()->GetOutput()->GetPointData()->HasArray("ARRAYNAME")) {
    std:cout << "An array named ARRAYNAME was found" << std::endl;
  }

  return EXIT_SUCCESS;
}

What am I doing wrong? What would be the correct way of avoid this error?

Thank you very much.

I think you’ll have to

#include “vtkPolyData.h”

Nicoco, after

reader->SetFileName(argv[1]);

insert the line

reader->Update();

This method is necessary to actually perform the reading and update correctly reader->GetOutput().

One question: what are you going to do with reader->GetOutput() ?
Maybe, you could do something like (this will copy your data):

vtkNew< vtkPolyData > polyData;
polyData->DeepCopy(reader->GetOutput());

Or simply

vtkSmartPointer< vtkPolyData > polyData = reader->GetOutput(); // No copy here

And then use the object ’ polyData’ for the rest of your processing / visualization pipeline.

Hope this help.

    Luca

Thank you for your help. Unfortunately, this does not solve the compile-time issue at all. I first tried just adding include. Here is what the code looks like now:

#include "vtkPolyDataReader.h"
#include "vtkNew.h"
#include "vtkPolyData.h"

int main(int argc, char *argv[]) {
  std::cout << "Reading file" << std::endl;
  
  vtkNew<vtkPolyDataReader> reader;
  reader->SetFileName(argv[1]);
  reader->Update();

  vtkSmartPointer< vtkPolyData > polyData = reader->GetOutput();

  int hasArray = polyData->GetPointData()->HasArray("ARRAYNAME");

  if (hasArray) {
    std:cout << "An array named ARRAYNAME was found" << std::endl;
  }

  return EXIT_SUCCESS;
}

and the error:

NicocoTest.cxx: In function ‘int main(int, char**)’:
NicocoTest.cxx:14:42: error: invalid use of incomplete type ‘class vtkPointData’
   int hasArray = polyData->GetPointData()->HasArray("ARRAYNAME");
                                          ^~
In file included from VTK-8.2.0/Common/DataModel/vtkPointSet.h:33,
                 from Common/DataModel/vtkPolyData.h:62,
                 from NicocoTest.cxx:3:
VTK-8.2.0/Common/DataModel/vtkDataSet.h:52:7: note: forward declaration of ‘class vtkPointData’
 class vtkPointData;
       ^~~~~~~~~~~~
make[2]: *** [nicoco/CMakeFiles/NicocoTest.dir/build.make:63: nicoco/CMakeFiles/NicocoTest.dir/NicocoTest.cxx.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:1233: nicoco/CMakeFiles/NicocoTest.dir/all] Error 2
make: *** [Makefile:95: all] Error 2

What I am trying to achieve seems so basic…

This error pretty much always means that header inclusions are missing. In VTK, the header name is (in the vast majority of cases) classname.h, so here, adding #include "vtkPointData.h" would make the class declaration available.

1 Like

Thank you very much @ben.boeckel

That was it!