如何在qt c ++中重载子类中的超类方法

时间:2015-12-06 15:04:35

标签: c++ qt inheritance

我的超类是Materials和FictionSection类继承Materials类。我试图将记录插入数据库。我已经创建了单独的类来处理用户界面和处理mysql查询。 materialinner.cpp中的addMaterials()在fictioninner.cpp中重载。为了处理图形界面,使用了fictionsection.cpp。为了实现insert方法,使用了functioninner.cpp。编译项目后,会出现以下错误。

H:\c++Prac\Libro\fictioninner.cpp:33: error: invalid operands of types 'const char*' and 'const char [4]' to binary 'operator+'
                  "values('"+ id +"','"+ copies +"','"+ shelf +"','"+ editn +"')");
                              ^

materials.h

#ifndef MATERIALS_H
#define MATERIALS_H

#include <QMainWindow>
#include "materialinner.h"

class FictionSection;
class JournalSection;



namespace Ui {
class Materials;
}

class Materials : public QMainWindow, public MaterialInner
{
    Q_OBJECT
private:
    Ui::Materials *ui;
    FictionSection *fic ;
    JournalSection *journal;
    //StaffDashboard *dashbrd;

};

#endif // MATERIALS_H

materialinner.h

#ifndef MATERIALINNER_H
#define MATERIALINNER_H

#include<QString>
#include "databaseconnection.h"
#include "validation.h"

class MaterialInner : public DatabaseConnection, public Validation
{
public:
    MaterialInner();
  //  MaterialInner(QString,QString,QString,int,QString,QString);

    QString title,fname,lname,category,publisher,cost;
    int id;
    bool addMaterials(QString,QString,QString,QString,QString,QString);
    bool updateMaterials(QString,QString,QString,QString,QString,QString);
    bool deleteRecords(QString);
    bool flag;

private:


};

#endif // MATERIALINNER_H

materials.cpp

#include "materials.h"
#include "ui_materials.h"
#include "fictionsection.h"
#include "journalsection.h"

#include <QDebug>
#include <QMessageBox>

Materials::Materials(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Materials)
{
    ui->setupUi(this);
   // QObject ::connect(actionFiction_section,SIGNAL(triggered()),this,SLOT(on_actionFiction_section_triggered()));
}

Materials::~Materials()
{
    delete ui;
}

void Materials::on_btnAdd_clicked()
{


     QString title = ui->lneditTitle->text();
     QString fnme = ui->lneditFnme->text();
     QString lnme = ui->lneditLnme->text();
      QString cost = ui->lneditCost->text();
     // int csts = cost.toInt();

     QString pub = ui->lneditPub->text();
     QString categry = ui->cmbxCategry->currentText();

       MaterialInner mat ;
        dbConOpen();
   bool flag  = mat.addMaterials(title,fnme,lnme,cost,pub,categry);
       if(flag == true)
       {
           QMessageBox :: warning(this,"Save","Data Inserted successfully");
       }
       else
       {
          QMessageBox :: critical(this,"Error","Couldn't insert reords");
       }
       dbConClose();
}

materialinner.cpp

#include <QtSql>
#include <QMessageBox>
#include <QDebug>
#include "materialinner.h"

MaterialInner::MaterialInner()
{

}
/*MaterialInner :: MaterialInner(QString title, QString fname, QString lname, int cost, QString publisher, QString category)
{
    this->title = title;
    this->fname = fname;
    this->lname = lname;
    this->cost = cost;
    this->publisher = publisher;
    this->category = category;
}*/
bool MaterialInner :: addMaterials(QString ttle, QString fnme, QString lnme,QString cst, QString pub, QString cat)
{
  // MaterialInner(ttle,fnme,lnme,cst,pub,cat);
   QSqlQuery query ;


  flag = query.exec("insert into book ( material_title,auth_fname,auth_lname,cost,publisher,category )"
                    " values ('"+ttle+"','"+ fnme+"','"+lnme+"','"+cst+"','"+pub+"','"+cat+"')");
  return flag;

}

fictionsection.h

#ifndef FICTIONSECTION_H
#define FICTIONSECTION_H

#include <QMainWindow>
#include"materials.h"
#include "fictioninner.h"

    namespace Ui {
    class FictionSection;
    }

    class FictionSection : public QMainWindow,public FictionInner
    {
        Q_OBJECT

    public:
        explicit FictionSection(QWidget *parent = 0);
        ~FictionSection();

    private slots:
        void on_btnAdd_clicked();

    private:
        Ui::FictionSection *ui;
    };

    #endif // FICTIONSECTION_H

fictioninner.cpp

#include <QtSql>

#include "fictioninner.h"

FictionInner::FictionInner()
{

}
bool FictionInner :: addMaterials(int id, int copies,int shelf, int editn)
{
    QSqlQuery query;
   bool flag = query.exec("insert into fiction (material_id,no_of_cpy,shelf_num,edition)"
                 "values('"+ id +"','"+ copies +"','"+ shelf +"','"+ editn +"')");
     return flag;
}

fictionsection.cpp

#include <QMessageBox>
#include "fictionsection.h"
#include "ui_fictionsection.h"


FictionSection::FictionSection(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::FictionSection)
{
    ui->setupUi(this);
}

FictionSection::~FictionSection()
{
    delete ui;
}

void FictionSection::on_btnAdd_clicked()
{
    //Accepting user entered values
 QString id = ui->cmbxId->currentText();
 int matId;
 if(!id.isEmpty())
 {
   matId = id.toInt();
 }
 QString copies = ui->lneditCopies->text();
 int numOfCpy;
 if(!copies.isEmpty())
 {
   numOfCpy = copies.toInt();
 }
 int shelfNo = ui->spinbxShelf->value();
 int edition = ui->spinbxEditn->value();
 dbConOpen();
 flag = addMaterials(matId,numOfCpy,shelfNo,edition);
 if(flag == true)
 {
      QMessageBox :: information(this,"Save","Data Inserted successfully", QMessageBox ::Ok);
 }
 else
 {
     QMessageBox :: critical(this,"Save","Data Inserted successfully");
 }
 dbConClose();
}

虽然我试图找到很多错误但我找不到它。先感谢您  如果我使用query.bindValue()会不正确?

bool FictionInner :: addMaterials(int id, int copies, int shelf, int editn)
{
    QSqlQuery query;
  //  QString string = QString("values ('") +QString :: number(id) +"','"+ QString ::number(copies)+"','"+ QString ::number(shelf)+"','"+ QString ::number(editn) '" ");
   bool flag = query.exec("insert into fiction (material_id,no_of_cpy,shelf_num,edition)"
                 "values(:matId, :cpy, :shlf, :edtn)");
   query.bindValue(":matId",id);
   query.bindValue(":cpy",copies);
   query.bindValue(":shlf",shelf);
   query.bindValue(":edtn",editn);
     return flag;
}

2 个答案:

答案 0 :(得分:2)

对于一个简单的错误,这是很多不相关的代码。一个简单的错误,消息真正说明了一切。

问题是你不能像你那样连接字符串。首先,你不能添加&#34;带有其他字符串文字的字符串文字,其次添加一个字符串文字和一个整数不会做你期望它做的事情。

由于您使用Qt,因此可以使用QStringQString::number动态构建字符串。

实施例

QString string = QString("values('") + QString::number(id) + "','" + ...; 

请注意,一旦有了一个QString对象,编译器就会自动找到从字符文字到QString的正确转换,因此只有第一个字符串文字需要是QString个对象。 / p>

关于向字符串文字添加整数的问题。字符串文字是只读字符数组。对于任何数组a整数索引i,表达式a[i]等同于*(a + i)。现在我们可以很容易地看到,向字符串文字添加一个整数只是为字符串添加一个偏移量,并给出一个指向该偏移量的指针。

示例:

char foo[] = "hello world";
puts(foo);  // Prints "hello world\n"
puts(foo + 6);  // Prints "world\n"

在上面的例子中,'w'中的"world"位于字符串的第七个位置,即索引6。

答案 1 :(得分:1)

要克服上述错误,您可以使用query.bindValue()来阻止您将int输入值转换为字符串。

bool FictionInner :: addMaterials(int id, int copies, int shelf, int editn)
{
    QSqlQuery query;

    query.prepare("insert into fiction (material_id,no_of_cpy,shelf_num,edition)"
                 "values(:matId, :cpy, :shlf, :edtn)");
   query.bindValue(":matId",id);
   query.bindValue(":cpy",copies);
   query.bindValue(":shlf",shelf);
   query.bindValue(":edtn",editn);
   flag = query.exec();
     return flag;
}