# Forward Declaration error when using Qt

**URL:** https://discourse.vtk.org/t/forward-declaration-error-when-using-qt/10066
**Category:** Support
**Created:** [December 5, 2022, 9:28am UTC](https://discourse.vtk.org/t/forward-declaration-error-when-using-qt/10066 "2022-12-05T09:28:03Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![nnzzll](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nnzzll/32/6126_2.png) [@nnzzll](https://discourse.vtk.org/u/nnzzll)
#### Post date: [December 5, 2022, 9:28am UTC](https://discourse.vtk.org/t/forward-declaration-error-when-using-qt/10066/1 "2022-12-05T09:28:03Z")

</div>

Here is a simple example for my problem:

```auto
// test.h
#include <QObject>
#include <vtkSmartPointer.h>

class vtkImageData;

class test : public QObject
{
 private:
  vtkSmartPointer<vtkImageData> img = vtkSmartPointer<vtkImageData>::New();

 public:
  test(/* args */) = default;
  ~test() =default;
};

// test.cxx
#include "test.h"
#include "vtkImageData.h"

```

Everything works fine with above code, however, if I add a `Q_OBJECT` macro to `test` class:

```auto
class test : public QObject
{
  Q_OBJECT
 private:
  vtkSmartPointer<vtkImageData> img = vtkSmartPointer<vtkImageData>::New();

 public:
  test(/* args */) = default;
  ~test() =default;
};

```

the compiler will produce an error:`incomplete type 'vtkImageData' used in nested name specifier`.

It looks like an issue due to Qt moc mechanism. Here are my questions:

1. What exactly causes this kind of problem?
2. Is there a way to use forward declaration with `Q_OBJECT`? Or I can only include the header file when `Q_OBJECT` macro is used?

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [December 5, 2022, 2:46pm UTC](https://discourse.vtk.org/t/forward-declaration-error-when-using-qt/10066/2 "2022-12-05T14:46:48Z")

</div>

The problem here is that you are trying to instantiate a class without telling the compiler its definition.

Move the `vtkSmartPointer<vtkImageData>::New` to the cxx file to fix it.

The reason it works by adding the Q\_OBJECT macro is likely that triggers `moc` on the implementation which finds the `#include <vtkImageData.h>`.
