我的 MouseInteractorStyle:对“vtkInteractorStyleZoom ::New()”的未定义引用

My MouseInteractorStyle : undefined reference to `vtkInteractorStyleZoom ::New()'

我在做一个vtk项目 我想编写自己的鼠标交互器样式 我写了这个class

vtkInteractorStyleZoom.h

#include <vtkSmartPointer.h>
#include <vtkInteractorStyleTrackballCamera.h>

class vtkInteractorStyleZoom: public vtkInteractorStyleTrackballCamera
{
public:
  static vtkInteractorStyleZoom* New();
  vtkTypeMacro(vtkInteractorStyleZoom , vtkInteractorStyleTrackballCamera);

  virtual void OnLeftButtonDown();

};

vtkInteractorStyleZoom.cpp

#include "vtkInteractorStyleZoom.h"


vtkStandardNewMacro(vtkInteractorStyleZoom);

void vtkInteractorStyleZoom::OnLeftButtonDown()
{
    this->StartDolly();
}

这是我使用它的函数 class

void ReadDICOMSeriesQt::on_ZoomButton_clicked()
{
    vtkSmartPointer<vtkInteractorStyleZoom> Style = 
vtkSmartPointer<vtkInteractorStyleZoom>::New();
    ui->qvtkWidget->GetRenderWindow()->GetInteractor()-
>SetInteractorStyle(Style);
}

当我用 cmake 编译我的项目时,我遇到了这个问题

CMakeFiles\ReadDICOMSeriesQt.dir/objects.a(ReadDICOMSeriesQt.cxx.obj): 在函数中 ZN15vtkSmartPointerI22vtkInteractorStyleZoomE3NewEv': C:/VTK/VTK-7.0.0/Common/Core/vtkSmartPointer.h:117: undefined reference tovtkInteractorStyleZoom::New()'

我不知道 cmake 是否有任何问题(也许我必须在我的 CMakeLists.txt 中更改一些东西)

有没有人可以帮助我?

您缺少的是构造函数定义。构造函数必须不带任何参数,以便对象工厂机制起作用。将其设为私有,因为没有人应该直接调用它。 您还应该禁用 class 的复制语义。即

class vtkInteractorStyleZoom : public vtkInteractorStyleTrackballCamera
{
  public:
    static vtkInteractorStyleZoom* New();
    vtkTypeMacro(vtkInteractorStyleZoom, vtkInteractorStyleTrackballCamera);

    virtual void OnLeftButtonDown();

 private:
   vtkInteractorStyleZoom() { /* definition, if any */ }

   vtkInteractorStyleZoom(const vtkInteractorStyleZoom&) = delete;
   void operator =(const vtkInteractorStyleZoom&) = delete;
};

此外,您应该 #include <vtkObjectFactory.h> 在 .cpp 文件中——这是定义 vtkStandardNewMacro() 的地方。

然后确保调用者模块 (ReadDICOMSeriesQt) 链接到 vtkInteractorStyleZoom 所属的库。