Reputation: 105
I'm trying to make use of interfaces in Qt/C++, but I keep getting a compile error which makes no sense to me, because I have implemented the virtual functions in the derived class. So I was hoping for some help :) Oh yeah, and the compile error is "cannot declare variable 'w' to be of abstract type 'Widget' because the following virtual functions are pure within 'Widget': startGame(), getBoard(), gameFinished().
Main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
ADTType.h
#ifndef ADTTYPE_H
#define ADTTYPE_H
class ADTType
{
public:
virtual void startGame() = 0;
virtual void getBoard() = 0;
virtual void gameFinished() = 0;
virtual ~ADTType() {}
};
Q_DECLARE_INTERFACE(ADTType, "ADTType")
#endif // ADTTYPE_H
Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSignalMapper>
#include <QHBoxLayout>
#include <QPushButton>
#include "ADTType.h"
class Widget : public QWidget,
public ADTType
{
Q_OBJECT
Q_INTERFACES(ADTType)
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
QHBoxLayout * mainLayout;
QPushButton * startButton;
QPushButton * buttons[10];
QSignalMapper * map;
};
#endif // WIDGET_H
Widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent)
{}
Widget::~Widget()
{}
void Widget::startGame() {
for (int i = 0; i < 10; i++) {
buttons[i]->setStyleSheet("QPushButton {background-color: normal}");
}
setButtons(true);
startButton->setDisabled(true);
}
void Widget::getBoard() {
bool * cells = game->getCells();
for (int i = 0; i < 10; i++) {
if (!cells[i]) {
buttons[i]->setStyleSheet("QPushButton {background-color: red}");
buttons[i]->setEnabled(false);
}
}
}
void Widget::gameFinished() {
int answer = game->getAnswer();
buttons[answer]->setStyleSheet("QPushButton {background-color: green}");
setButtons(false);
game->reset();
QMessageBox::information(this,tr("Finito"),tr("You found it."),
QMessageBox::Ok);
startButton->setEnabled(true);
}
Upvotes: 0
Views: 629
Reputation: 2376
You derived ADTType
abstract class from the Widget
so you need to override pure virtual functions in the Widget
class otherwise it also will become abstract.
class Widget : public QWidget,
public ADTType
{
...
void startGame() { ... }
void getBoard() { ... }
void gameFinished() { ... }
};
Upvotes: 1
Reputation: 56567
class Widget : public QWidget,
public ADTType
So Widget
inherits from the abstract class ADTType
. However, you don't declare the overrides (startGame()
etc) in your Widget
class, but you only define them in the Widget.cpp
file. You also need to declare them in the Widget.h
class header
// need these declarations inside the class Widget in Widget.h
virtual void startGame();
virtual void getBoard();
virtual void gameFinished();
Minimal example here.
Upvotes: 3