VTK/Tutorials/MemberVariables

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search

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:

class vtkDenseArray;

but forgotten to actually include the header in your implementation file:

#include <vtkDenseArray.h>

== error: 'vtkDenseArray' is not a template error: template argument required for 'class vtkDenseArray' ==

MyClass.h

#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

MyClass.cxx

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

NonSmartPointerMember.cxx

#include <vtkSmartPointer.h>
#include "MyClass.h"

int main(int argc, char *argv[])
{
  vtkSmartPointer<vtkMyClass> test = vtkSmartPointer<vtkMyClass>::New();

  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

project(NonSmartPointerMember)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(NonSmartPointerMember NonSmartPointerMember.cxx MyClass.cxx)
if(VTK_LIBRARIES)
  target_link_libraries(NonSmartPointerMember ${VTK_LIBRARIES})
else()
  target_link_libraries(NonSmartPointerMember vtkHybrid)
endif()