I am having problem in using a virtual function across several files.

I have this code:

myPlugin.h

#include "myprojPluginInterface.h"
#include "myproj_export.h"

class MYPROJ_EXPORT myProjPlugin : public myprojPluginInterface
{

 public:
    virtual ~myProjPlugin();
    virtual myProjPlugin * newInstance() const = 0;

    virtual int speed() const;
    virtual int direction() const;
       
 protected:
    PositionProviderPlugin();
   
};

}

I want to use speed() and direction() in another class otherPlugin
otherPlugin.h

#include "myProjPlugin.h"

class otherPlugin: public myProjPlugin
{
public: 
void update(int speed, int direction);
private:
int m_speed;
int m_direction;
}

How can I invoke speed() and direction() from otherPlugin.cpp ?

I have tried this.

otherplugin.cpp

#include<otherPlugin.h>

void otherPlugin::update(int speed, int direction)
{
  m_speed = speed;
  m_direction = direction;
}

int myProjPlugin::speed() const   //want suggestion here
{
   return m_speed;
}

int myProjPlugin::direction() const  //want suggestion here
{
   return m_direction;
}

I get the following error:

error: ‘m_speed’ was not declared in this scope
error: ‘m_direction’ was not declared in this scope

How can I accomplish this ? Is there any better way so that I can use something like int otherPlugin::speed() const ?

You have to define the virtual functions in their derived classes.

int myProjPlugin::speed() const   //want suggestion here
{
   return m_speed;
}
 
int myProjPlugin::direction() const  //want suggestion here
{
   return m_direction;
}

error: ‘m_speed’ was not declared in this scope
error: ‘m_direction’ was not declared in this scope

m_speed & m_direction aren't defined in myProjPlugin hence the error.
define the functions int otherPlugin::speed() const & int otherPlugin::direction() const

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.