Reputation: 189
I want to implement an abstract class, but can't override purely virtual functions. For this reason compiler thinks of my class AddDialog_av as an abstract one.
It seems that compiler doesn't see the definitions in AddDialog_av.cpp file.
AddDialog.h (supposed to be an abstract class):
class AddDialog : public QDialog
{
Q_OBJECT
public:
AddDialog(QWidget *parent = 0);
virtual ~AddDialog();
protected:
virtual QString generateId()=0;
virtual bool validate()=0;
private slots:
void addButton_clicked();
void cancelButton_clicked();
};
AddDialog_av.h (supposed to be instantiated):
#include <adddialog.h>
class AddDialog_av : public AddDialog
{
public:
AddDialog_av();
};
AddDialog_av.cpp:
#include <adddialog_av.h>
bool AddDialog_av::validate()
{
return true;
}
QString AddDialog_av::generateId()
{
return "Fork out of here.";
}
main.cpp
#include "adddialog_av.cpp"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
AddDialog_av w;
w.show();
return a.exec();
}
List of errors:
adddialog_av.cpp:3: error: no 'bool AddDialog_av::validate()' member function declared in class 'AddDialog_av'
bool AddDialog_av::validate()
^
adddialog_av.cpp:8: error: no 'QString AddDialog_av::generateId()' member function declared in class 'AddDialog_av'
QString AddDialog_av::generateId()
^
main.cpp:7: error: cannot declare variable 'w' to be of abstract type 'AddDialog_av'
AddDialog_av w;
^
Upvotes: 1
Views: 620
Reputation: 360
If you have =0 you must derived and implement base class AddDialog. So add
class MyAddDialog : public AddDialog
{
protected:
QString generateId(){
qDebug() << "works :)";
};
virtual bool validate(){
qDebug() << "works to :) :)";
};
};
and use:
MyAddDialog myAddDialog;
in code. Then it will run. Do not declare
AddDialog addDialog; // it will not works!!!!
Upvotes: 0
Reputation: 726589
You must tell the compiler of the override in the declaration of your AddDialog_av
class. Providing an implementation the way you tried to do, i.e. specifying it in the CPP file alone, would not work, because the compiler does not even know that your AddDialog_av
class has AddDialog_av::generateId()
and AddDialog_av::validate
functions in the first place. The fact that AddDialog_av
inherits them from a base class AddDialog
does not mean that you plan to provide an implementation in the derived class.
You must add the function to the header file as well:
class AddDialog_av : public AddDialog
{
public:
AddDialog_av();
protected:
virtual QString generateId();
virtual bool validate();
};
Upvotes: 3