VTK/Tutorials/MemberVariables
Here are some common errors when trying to use a templated VTK class as a member variable. We will use vtkDenseArray as an example.
error: 'vtkDenseArray' is not a template
You have forward declared the class in your header: <source lang="cpp"> class vtkDenseArray; </source>
but forgotten to actually include the header in your implementation file:
<source lang="cpp">
- include <vtkDenseArray.h>
</source>
== error: 'vtkDenseArray' is not a template error: template argument required for 'class vtkDenseArray' ==
MyClass.h
<source lang="cpp">
- ifndef __vtkMyClass_h
- define __vtkMyClass_h
- include "vtkPolyDataAlgorithm.h" //superclass
class vtkDenseArray;
class vtkMyClass : public vtkPolyDataAlgorithm {
public:
static vtkMyClass *New(); vtkTypeRevisionMacro(vtkMyClass,vtkObject); void PrintSelf(ostream &os, vtkIndent indent);
protected:
vtkMyClass(); ~vtkMyClass(); int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *); //the function that makes this class work with the vtk pipeline
private:
vtkDenseArray<double>* OutputGrid;
};
- endif
</source>
MyClass.cxx
<source lang="cpp">
- include "vtkObjectFactory.h" //for new() macro
- include "vtkInformation.h"
- include "vtkInformationVector.h"
- include "vtkDenseArray.h"
- include "MyClass.h"
vtkCxxRevisionMacro(vtkMyClass, "$Revision: 1.1 $"); vtkStandardNewMacro(vtkMyClass);
vtkMyClass::vtkMyClass() {
this->OutputGrid = vtkDenseArray<double>::New();
}
vtkMyClass::~vtkMyClass() {
this->OutputGrid->Delete();
}
int vtkMyClass::RequestData(vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector, vtkInformationVector *outputVector)
{
return 1;
}
void vtkMyClass::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
} </source>
NonSmartPointerMember.cxx
<source lang="cpp">
- include <vtkSmartPointer.h>
- include "MyClass.h"
int main(int argc, char *argv[]) {
vtkSmartPointer<vtkMyClass> test = vtkSmartPointer<vtkMyClass>::New();
return 0;
}
</source>
CMakeLists.txt
<source lang="text"> cmake_minimum_required(VERSION 2.6)
PROJECT(NonSmartPointerMember)
FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(NonSmartPointerMember NonSmartPointerMember.cxx MyClass.cxx) TARGET_LINK_LIBRARIES(NonSmartPointerMember vtkHybrid)
</source>