VTK/Tutorials/MemberVariables: Difference between revisions

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
(New page: 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 f...)
 
No edit summary
Line 14: Line 14:


==
==
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>

Revision as of 13:58, 13 November 2009

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="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>