I believe this might be an issue of #including or forward declaring, rather than an issue of my syntax, since I’m currently getting errors of “invalid use of incomplete type ‘struct MainWindow'”, and “forward declaration of ‘struct MainWindow’ when I attempt to build the following in Qt Creator (Qt 4.7.4):
MYCLASS.H
#ifndef MYCLASS_H
#define MYCLASS_H
class MainWindow;
class MyClass
{
public:
MyClass(MainWindow * parent);
void callParentFunction();
private:
MainWindow *myPointer;
};
#endif // MYCLASS_H
MYCLASS.CPP
#include "myclass.h"
MyClass::MyClass(MainWindow *parent) : myPointer(parent)
{
}
void MyClass::callParentFunction()
{
myPointer->setSpinBoxValue(500);
}
MAINWINDOW.H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDoubleSpinBox>
#include "myClass.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
void setSpinBoxValue(double x);
private:
QDoubleSpinBox *mySpinBox;
MyClass *myObject;
};
#endif // MAINWINDOW_H
MAINWINDOW.CPP
#include "mainwindow.h"
MainWindow::MainWindow()
{
mySpinBox = new QDoubleSpinBox;
setCentralWidget(mySpinBox);
myObject = new MyClass(this);
myObject->callParentFunction();
}
void MainWindow::setSpinBoxValue(double x)
{
mySpinBox->setValue(x);
}
I’d appreciate any ideas. Thanks!
You need to include
mainwindow.hinmyclass.cppaftermyclass.h. Inmyclass.cppyou call a method of theMyClass(insideMyClass::callParentFunction), but at that point the compiler still only has the forward-declaration ofMainWindow.