why definition is talking from .cpp file?
-
AbstractExperiment.h
class AbstractExperiment { public: AbstractExperiment() {}; virtual QString GetShortName() const = 0; virtual QWidget* CreateUserInput() const = 0; virtual QString GetExpParamTxt(QWidget*)const; }; Q_DECLARE_METATYPE(AbstractExperiment*) Q_DECLARE_METATYPE(const AbstractExperiment*)
AbstractExperiment.cpp
QString AbstractExperiment::GetExpParamTxt(QWidget*) const { QString ret = ""; return ret; }
ExperimentFactoryInterface.h
#pragma once #include <QVariant> #include <AbstractExperiment.h> class ExperimentFactoryInterface { public: virtual ~ExperimentFactoryInterface() {} virtual AbstractExperiment* CreateExperiment(const QVariant& = QVariant()) = 0; }; Q_DECLARE_INTERFACE(ExperimentFactoryInterface, "ExperimentFactoryInterface")
Experiment.h
#ifndef CYCLICVOLTAMMETRY_H #define CYCLICVOLTAMMETRY_H #include <AbstractExperiment.h> #include <QtWidgets/QWidget> class StaircaseVoltammetry : public AbstractExperiment { public: QString GetShortName() const; QWidget* CreateUserInput() const; QString GetExpParamTxt(QWidget* wdg)const ; };
Factory.h
#pragma once #include <ExperimentFactoryInterface.h> class Factory : public QObject, public ExperimentFactoryInterface { Q_OBJECT Q_PLUGIN_METADATA(IID "ExperimentFactoryInterface") Q_INTERFACES(ExperimentFactoryInterface) public: AbstractExperiment* CreateExperiment(const QVariant& = QVariant()); };
I have
virtual QString GetExpParamTxt(QWidget*)const;
function inAbstractExperiment.h
, which is not pure virtual function.If I will right the definition
GetExpParamTxt
inAbstractExperiment.h
is compile perfectly but I define the function definition inAbstractExperiment.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?
-
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
.Here few other static function I mention in
AbstractExperiment.h
it is also giving me error if i will write definition inAbstractExperiment.cpp
. In general, If I will define the function inAbstractExperiment.h
file then Experiment file is able to read the definition from the base class. if I will mention definition inAbstractExperiment.cpp
then derived class does not able to read the definition of base class function. -
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 fromAbstractExperiment
, 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 classAbstractExperiment
.just now I try
Q_DECL_EXPORT virtual QString GetExpParamTxt(QWidget*)const;
. I got same error. -
@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.