Home > Software engineering >  How can I access functions of this abstract class?
How can I access functions of this abstract class?

Time:12-15

Im currently working with an UI tool (Qt Creator 9.5.9) to create UI Interfaces. While messing with the tool i came across following problem: The following code is from an automatically generated cpp file which is generated when creating a new project. At the top there are a few functions which I assume can be used to access and possibly change data points. I want to use the function SetWriteDP() to write my data to the data points.

/**
    // register ids
    bool registerReadIds(const QList<unsigned int> &ids);
    bool registerReadIds(const QUintSet &ids);
    bool registerReadIds(const QUintSet &ids, void (*func)(void*, const QUintSet &));
    bool registerWriteIds(const QList<unsigned int> &ids);
    bool registerWriteIds(const QUintSet &ids);

    // read data point values
    unsigned int GetReadDP(const unsigned int &id) const;
    int GetReadDPInt(const unsigned int &id) const;
    float GetReadDPFloat(const unsigned int &id) const;
    QString GetReadDPString(const unsigned int &id) const;

    // write data point values
    void SetWriteDP(const unsigned int &id, const unsigned int &value);
    void SetWriteDP(const unsigned int &id, const int &value);
    void SetWriteDP(const unsigned int &id, const float &value);
    void SetWriteDP(const unsigned int &id, const QString &value);

    // execute sql statement
    QSqlQuery execSqlQuery(const QString &query, bool &success) const;



**/

#include "hmi_api.h"
#include "widget.h"
#include "ui_arbaseform.h"
#include <iostream>


HMI_API::HMI_API(QWidget *parent) :
    AbstractAPI(parent), m_ui(NULL)
{
    Widget *widget = dynamic_cast<Widget *>(parent);
    if(!widget) return;
    m_ui = widget->ui;
    QUintSet readIdsToRegister, writeIdsToRegister;
    writeIdsToRegister.insert(10001);
    registerReadIds(readIdsToRegister);
    registerWriteIds(writeIdsToRegister);
    SetWriteDP(100001, 69);
}

I tried using the function in another cpp file in different ways:

HMI_API.SetWriteDP() HMI_API.Abstract_API.SetWriteDP() This resulted in this error: expected unqualified-id before . token

AbstractAPI::SetWriteDP() which resulted in this error: cannot call member function 'void DPObject::SetWriteDP(const unsigned int&, const int&, unsigned int)' without object AbstractAPI::SetWriteDP();

the i tried making a DPObject which resulted in this error: cannot declare variable 'test' to be of abstract type 'DPObject'

Im really at my wits end now how to access this function. Can someone maybe explain to me what happens after "HMI_API::HMI_API(QWidget *parent) :" and why it is possible to use the function in that block and how i can make it possible for me to use this function.

I tried reading the documentation but nowwhere in the documentation this function is ever mentioned.

The function works in the code snippet i posted but doesnt when i want to use it in another function, i know its because of some stuff regarding classes but im dont understand how to work around this in this case.

Thanks in advance!

CodePudding user response:

how i can make it possible for me to use this function.

I might be wrong but from my understanding of C you would first have to create an object of the class, in this case that would be

HMI_API *uiName = new HMI_API(some_parent_obj);

With QWidget being your earlier created QWidget, you can then call the function using a .

uiName.SetWriteDP(x,y);

Can someone maybe explain to me what happens after "HMI_API::HMI_API(QWidget *parent) :

after "HMI_API::HMI_API(QWidget *parent)" the class makes it clear that it inherits base functionality from the classes AbstractAPI and m_ui, you can learn more about inheritance here :https://www.learncpp.com/cpp-tutorial/basic-inheritance-in-c/

Afterwards im not sure but it looks like it just creates some basic functionality so you can call the functions using the class.

CodePudding user response:

I found an answer to my problem which in hindsight might have been obvious but i dont know how i should have known.

I was able to use the functions by declaring a new function like this:

void HMI_API::myFunction(int arg1){
     my code with the functions i wanted to use
}

I really hope this will help someone that might had some understanding problems as well.

  • Related