Unable to use static function in a c++ program using Qt Creator.



  • Hello . I am new to Qt and I'm trying to make a database program in C++ using Qt Creator to provide a GUI.
    As a part of my assignment in school, the requirement is to use a static member function , so i decided to add one to initialise my static data member with -1 , so as to use it to count the number of times the function x() is called on pushing a button . The error I am getting is this :

    " expected constructor, destructor or type conversion before ';' token.
    (In line 25 of the .cpp file )

    Here is the code for the database.h and database.cpp file :

    @#include <QtGui/QMainWindow>
    #include "ui_database.h"
    #include<QMessageBox>

    class database : public QMainWindow
    {
    Q_OBJECT

    public:
    database(QWidget *parent = 0);
    ~database();
    Ui::database ui;

    private:
    static int count; //static data member
    private slots:
    void x();
    void search();
    void modify();

    static void init() //static inline function definition
    {// int database::count=-1;//initialise the count to -1
    count=-1;
    }
    };

    #endif // DATABASE_H@

    .cpp file:

    @#include "database.h"
    #include <person.h>
    #include<QString>
    #include<string.h>
    #include<QMessageBox>
    #include<QStandardItem>

    database::database(QWidget *parent)
    : QMainWindow(parent)
    {
    ui.setupUi(this);
    connect(this->ui.pushButton,SIGNAL(clicked()),this,SLOT(x()));
    connect(this->ui.pushButton_2,SIGNAL(clicked()),this,SLOT(search()));
    connect(this->ui.pushButton_3,SIGNAL(clicked()),this,SLOT(modify()));
    }

    database::~database()
    {

    }
    person p[10];//array of objects

    int database::count;
    database::init();//calling the static,inline member function to initialise count
    void database ::x()
    {
    count++;//increment count to count the number of times button was pressed
    person s1,s2;

    QString x,q;
    int y;
    int z;
    long m;
    x = this->ui.textEdit->toPlainText();//to accept name
    y = this->ui.textEdit_3->toPlainText().toInt();
    z = this->ui.textEdit_7->toPlainText().toInt();
    q=this->ui.textEdit_12->toPlainText();//to accept blood grp
    m=this->ui.textEdit_11->toPlainText().toLong();//to accept phone number

    p[count].add(x,y,z,m,q);//calling the function of the second cpp class
    s2 = p[count].dis();

    this->ui.textEdit_4->append(s2.name);

    this->ui.textEdit_6->append(QString::number(s2.age));
    this->ui.textEdit_5->append(QString::number(s2.id));
    this->ui.textEdit_8->append(QString::number(s2.phone));
    this->ui.textEdit_15->append(s2.blood);

    //now clearing the text boxes after having displayed below

    this->ui.textEdit->setText("");
    this->ui.textEdit_3->setText("");
    this->ui.textEdit_7->setText("");
    this->ui.textEdit_12->setText("");
    this->ui.textEdit_11->setText("");
    }
    void database::search() //function to search according to the id
    {
    int id2;
    int found;
    int j;
    id2=this->ui.textEdit_2->toPlainText().toInt();
    for(j=0;j<=count;j++)
    {
    if(p[j].id==id2)
    {
    found=1;
    break;
    }
    else
    {
    found=0;
    }

    }
    if(found==1)
    {
    this->ui.textEdit_9->setText(p[j].name);
    this->ui.textEdit_10->setText(QString::number(p[j].age));
    this->ui.textEdit_13->setText(QString::number(p[j].phone));
    this->ui.textEdit_14->setText((p[j].blood));
    }
    else
    {
    QMessageBox msgBox;
    msgBox.setText("NOT FOUND");
    msgBox.exec();
    }
    }

    void database::modify()
    { int x2;// for age
    long ph2; // for phone number
    QString q1,q2;//for name and blood group
    int id2;//local var to take ID of data to be modified
    int found;
    int j;

    // int current_id;
    id2=this->ui.textEdit_2->toPlainText().toInt();
    for(j=0;j<=count;j++)
    {
    if(p[j].id==id2)
    {
    found=1;
    break;
    }
    else
    {
    found=0;
    }
    }

    if(found==1)
    {
        q1=this->ui.textEdit_9->toPlainText();//for new name in text box
        x2=this->ui.textEdit_10->toPlainText().toInt();//for new age in RHS Textbox
        ph2=this->ui.textEdit_13->toPlainText().toLong();//for new phone number in RHS textbox
        q2=this->ui.textEdit_14->toPlainText();//for new blood group :P
       person p2,p3;
       p[j].add(q1,x2,id2,ph2,q2);
       p3=p[j].dis();
       this->ui.textEdit_4->append(p3.name);
    
       this->ui.textEdit_6->append(QString::number(p3.age));
       this->ui.textEdit_5->append(QString::number(p3.id));
       this->ui.textEdit_8->append(QString::number(p3.phone));
       this->ui.textEdit_15->append(p3.blood);
    
      this->ui.textEdit_2->setText("");
       this->ui.textEdit_9->setText("");
       this->ui.textEdit_10->setText("");
       this->ui.textEdit_13->setText("");
       this->ui.textEdit_14->setText("");
    
    
    }
    

    }
    @

    Need your help as i really need to do this assignment for my school project . Thanks.



  • The mistake I made here was that I was trying to call the static member function globally , instead of doing so in a function or in the main . Sorry for having posted this here . It was purely a mistake in c++ programming.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.