Important: Please read the Qt Code of Conduct -

why definition is talking from .cpp file?

  • AbstractExperiment.h

    class AbstractExperiment {
        AbstractExperiment() {};
        virtual QString GetShortName() const = 0;
        virtual QWidget* CreateUserInput() const = 0;
    	virtual QString GetExpParamTxt(QWidget*)const;
    Q_DECLARE_METATYPE(const AbstractExperiment*)


    QString AbstractExperiment::GetExpParamTxt(QWidget*) const {
    	QString ret = ""; 
    	return ret; 


    #pragma once
    #include <QVariant>
    #include <AbstractExperiment.h>
    class ExperimentFactoryInterface {
    	virtual ~ExperimentFactoryInterface() {}
    	virtual AbstractExperiment* CreateExperiment(const QVariant& = QVariant()) = 0;
    Q_DECLARE_INTERFACE(ExperimentFactoryInterface, "ExperimentFactoryInterface")


    #include <AbstractExperiment.h>
    #include <QtWidgets/QWidget>
    class StaircaseVoltammetry : public AbstractExperiment {
    	QString GetShortName() const;
    	QWidget* CreateUserInput() const;
    	QString GetExpParamTxt(QWidget* wdg)const ;


    #pragma once
    #include <ExperimentFactoryInterface.h>
    class Factory : public QObject, public ExperimentFactoryInterface {
    	Q_PLUGIN_METADATA(IID "ExperimentFactoryInterface")
    	AbstractExperiment* CreateExperiment(const QVariant& = QVariant());

    I have virtual QString GetExpParamTxt(QWidget*)const; function in AbstractExperiment.h, which is not pure virtual function.

    If I will right the definitionGetExpParamTxt in AbstractExperiment.h is compile perfectly but I define the function definition in AbstractExperiment.cpp then I am getting error like below.


    I am creating dll file for Experiment file.

    what is change do I require for make it work?

  • does AbstractExperiment.h have #include <QWidget>?

  • @VRonin
    Yes it is include through another file. Like I have #include <ExperimentUIHelper.h> contain #include <QWidget>, and AbstractExperiment.h contain #include <ExperimentUIHelper.h>.

  • Lifetime Qt Champion

    When you create a shared library you have to export your functions you want to use from outside since by default all symbols are hidden on windows (in contrast to linux where all is visible)

  • Here I mention compiler output. ignore about other virtual function. it is program same as GetExpParamTxt. 0_1557250473955_Capture.PNG

    Here few other static function I mention in AbstractExperiment.h it is also giving me error if i will write definition in AbstractExperiment.cpp. In general, If I will define the function in AbstractExperiment.h file then Experiment file is able to read the definition from the base class. if I will mention definition in AbstractExperiment.cpp then derived class does not able to read the definition of base class function.

  • @Christian-Ehrlicher

    I know about Q_DECL_EXPORT. Here what I am doing. I am including AbstractExperiment.h in main project. and experiment is created in another sub project, sub project class is derived from AbstractExperiment, and derived class is used functionality of base class (AbstractExperiment).

    just curious to know. If AbstractExperiment is not exporting function then why it is working if I will define the definition inside the class AbstractExperiment.

    just now I try Q_DECL_EXPORT virtual QString GetExpParamTxt(QWidget*)const;. I got same error.

  • Lifetime Qt Champion

    @Yash001 said in why definition is talking from .cpp file?:

    why it is working if I will define the definition inside the class

    Because then the compiler sees the definition when you compile your executable.

    just now I try Q_DECL_EXPORT virtual QString GetExpParamTxt(QWidget*)const;. I got same error.

    You did not read the link carefully... you have to export it when compiling the lib, import it when linking against as explained.