Reputation: 251
I am working on an IDE written in QT. I need to use the QMainWindow AND the QSyntaxHighLighter classes. However, when compiling it spits out the following error.
cannot declare variable 'w' to be of abstract type 'SquareSDK'
That refers to my main.cpp file.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SquareSDK w;
w.show();
return a.exec();
}
That class is defined in ediot.h
#ifndef EDIOT_H
#define EDIOT_H
#include <QMainWindow>
#include <QFileDialog>
#include <QtCore>
#include <QtGui>
#include <QObject>
#include <QSyntaxHighlighter>
namespace Ui {
class SquareSDK;
}
class SquareSDK : public QMainWindow, private QSyntaxHighlighter
{
Q_OBJECT
public:
explicit SquareSDK(QWidget *parent = 0);
~SquareSDK();
private slots:
void on_actionUndo_triggered();
void on_actionRedo_triggered();
void on_actionCut_triggered();
void on_actionCopy_triggered();
void on_actionPaste_triggered();
void on_actionNew_triggered();
void on_actionOpen_triggered();
void on_actionSave_triggered();
void on_actionSave_As_triggered();
void on_actionRun_in_Default_Web_Browser_triggered();
void on_actionReload_triggered();
void test();
void syntax();
private:
Ui::SquareSDK *ui;
QString mFilename;
QString urlname;
QString urldebug;
QString OS;
QString a;
QString b;
QString error;
QString ext;
QString text;
};
#endif // EDIOT_H
Upvotes: 0
Views: 3085
Reputation: 578
As one or both of base classes have 1 or more pure virtual methods, until you implement them in SquareSDK
, SquareSDK
is an abstract class (because it literally inherits those pure virtual methods). You can't instantiate an abstract class.
(Although you can use it as a type for pointer or reference. But it can only point to its non-abstract sub-classes, despite pointer-type being of abstract class. But that's not important right now)
I checked out the documentation for QSyntaxHighlighter
, and it has 1 pure virtual method.
//this is how it looks like inside QSyntaxHighlighter class
virtual void highlightBlock (const QString& text) = 0;
So implement it in your SquareSDK:
//in .h
void highlightBlock (const QString& text);
//in .cpp
void SquareSDK::highlightBlock (const QString& text)
{
//...yourcode...
}
It's a function for highlighting blocks, and because it's pure virtual, it has absolutely no original behavior defined so you must program it completely on your own. So implement it in a way it highlights a block the way you want it highlighted.
QMainWindow
, luckily, contains no pure virtual methods, which cuts you some slack.
EDIT:
Inherit QSyntaxHighlighter as public. highlightBlock
is protected, and if base class is inherited as private, it becomes unreachable.
Upvotes: 2
Reputation: 37697
Double QObject
inheritance is forbidden in Qt
! So you can't do it with QMainWindow
and QSyntaxHighlighter
. In this case double inheritance is bad in many many other ways!
Use composition not aggregation here!
Upvotes: 2