VTK/Tutorials/MemberVariables

From KitwarePublic
< VTK‎ | Tutorials
Revision as of 18:49, 6 May 2014 by Christopher.mullins (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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: <source lang="cpp"> class vtkDenseArray; </source>

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

<source lang="cpp">

  1. include <vtkDenseArray.h>

</source>

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

MyClass.h

<source lang="cpp">

  1. ifndef __vtkMyClass_h
  2. define __vtkMyClass_h
  1. 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;

};

  1. endif

</source>

MyClass.cxx

<source lang="cpp">

  1. include "vtkObjectFactory.h" //for new() macro
  2. include "vtkInformation.h"
  3. include "vtkInformationVector.h"
  4. include "vtkDenseArray.h"
  1. 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">

  1. include <vtkSmartPointer.h>
  2. include "MyClass.h"

int main(int argc, char *argv[]) {

 vtkSmartPointer<vtkMyClass> test = vtkSmartPointer<vtkMyClass>::New();
 return 0;

}

</source>

CMakeLists.txt

<source lang="cmake"> 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() </source>