Results 1 to 3 of 3

Thread: Problem with my C++ library that uses a interface class

  1. #1
    Join Date
    Jun 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with my C++ library that uses a interface class

    hi,i 'm creating a C++ library that inherits from an interface,but i have met some troubles during runtime.

    it is my Interface class
    Qt Code:
    1. #ifndef TABLEMANAGERINTERFACE_H
    2. #define TABLEMANAGERINTERFACE_H
    3.  
    4. #include<QtPlugin>
    5.  
    6. #include <QtSql/QSqlRecord>
    7. #include <QtSql/QSqlTableModel>
    8. #include <QtSql/QSqlDatabase>
    9.  
    10. class TableManagerInterface
    11. {
    12.  
    13. private:
    14.  
    15. enum TypeModels { ReadOnlyModel,ReadWriteModel,RelationnalModel};
    16. public:
    17. virtual ~TableManagerInterface() {}
    18.  
    19. virtual void setDatabase(const QSqlDatabase &database)=0;
    20. virtual void setTable(const QString &tablename)=0;
    21. virtual void select(const QString &filter=0);
    22. virtual void insertRecord(const QSqlRecord &record)=0;
    23. //virtual void editRecord(const QSqlRecord &record) =0;
    24. // virtual void deleteRecord(const QSqlRecord &record) =0;
    25.  
    26.  
    27. };
    28.  
    29. QT_BEGIN_NAMESPACE
    30.  
    31. Q_DECLARE_INTERFACE(TableManagerInterface,
    32. "com.MaxSoft.Plugin.TableManagerInterface/1.0")
    33.  
    34. QT_END_NAMESPACE
    35.  
    36. #endif
    To copy to clipboard, switch view to plain text mode 

    now the defintion of my library class

    ItableManager.h
    Qt Code:
    1. #ifndef ITABLEMANAGER_H
    2. #define ITABLEMANAGER_H
    3.  
    4. #include "ITableManager_global.h"
    5. #include <QtGui/QStylePlugin>
    6. #include<QtSql>
    7. #include "interface/TableManagerInterface.h"
    8.  
    9.  
    10. class ITABLEMANAGERSHARED_EXPORT ITableManager :public QObject,public TableManagerInterface
    11. {
    12. Q_OBJECT
    13. Q_INTERFACES(TableManagerInterface)
    14.  
    15. public:
    16. ITableManager();
    17. virtual ~ITableManager() {}
    18.  
    19. private:
    20. QMap<int,QString> mapKey;
    21. public:
    22. void setDatabase(const QSqlDatabase &database);
    23. void setTable(const QString &tablename);
    24. void setPrimaryKeys(QMap<int, QString> map);
    25. void select(const QString &filter=0);
    26. QSqlRecord record() const;
    27. void insertRecord(const QSqlRecord &record);
    28.  
    29. signals:
    30.  
    31. public slots:
    32.  
    33.  
    34.  
    35. };
    36.  
    37. #endif // ITABLEMANAGER_H
    To copy to clipboard, switch view to plain text mode 

    ITableManager.cpp

    Qt Code:
    1. #include "itablemanager.h"
    2. #include<QMessageBox>
    3.  
    4.  
    5. ITableManager::ITableManager()
    6. {
    7.  
    8. }
    9.  
    10.  
    11. /*!
    12.  Cette méthode permet de récupérer une instance de laconnexion
    13.   à une base de donnée
    14. */
    15. void ITableManager::setDatabase(const QSqlDatabase &database)
    16. {
    17. this->db=database;
    18. }
    19.  
    20. /*!
    21.  
    22. informe sur la table et ses champs
    23.   */
    24. void ITableManager::setTable(const QString &tablename)
    25. {
    26. model= new QSqlTableModel(0,this->db);
    27. model->setTable(tablename);
    28. }
    29.  
    30. /*!
    31.   charge le model avec les lignes
    32. */
    33. void ITableManager::select(const QString &filter)
    34. {
    35. model->setFilter(filter);
    36. if( !model->select() )
    37. qDebug()<<" Lignes non chargées";
    38. }
    39.  
    40. /*!
    41.   retourne une enregistrement contenant les infos Ã
    42.   propos des colonnes(champs) de la table
    43.   */
    44. QSqlRecord ITableManager::record() const
    45. {
    46. return model->record();
    47. }
    48.  
    49. /*!
    50.   spécifie les clés primaires et leurs types
    51.   */
    52.  
    53. void ITableManager::setPrimaryKeys(QMap<int, QString> map)
    54. {
    55. mapKey = map;
    56. }
    57.  
    58. /*!
    59.  
    60.  */
    61.  
    62. void ITableManager::insertRecord(const QSqlRecord &record)
    63. {
    64.  
    65. //on verifie si la clé existe
    66. QString filter="WHERE ";
    67.  
    68. QMapIterator<int,QString> i(mapKey);
    69. while (i.hasNext()) {
    70. i.next();
    71. // cout << i.key() << ": " << i.value() << endl;
    72. if( i.value()=="int"){
    73.  
    74. filter += record.fieldName(i.key()) + "=" + record.value(i.key()).toInt()+ " ";
    75. }
    76. else if (i.value()=="String") {
    77.  
    78. filter += record.fieldName(i.key()) + "='" + record.value(i.key()).toString() +"' ";
    79. }
    80. else if (i.value()=="Date") {
    81. filter += record.fieldName(i.key()) + "='"+record.value(i.key()).toDate().toString("dd/MM/yyyy")+"' ";
    82.  
    83. }
    84. else if (i.value()=="Double") {
    85. filter += record.fieldName(i.key()) + "=" +record.value(i.key()).toDouble() + " ";
    86.  
    87. }
    88. if( i.hasNext())
    89. filter +=" AND ";
    90. }
    91.  
    92. this->select(filter);
    93.  
    94. if (model->rowCount() != 0)
    95. QMessageBox::warning(0,tr("Erreur Insertion"),tr("Présence de doublons"));
    96. else{
    97.  
    98. if( model->insertRecord(-1,record) )
    99. QMessageBox::information(0,tr("Insertion"),tr("Enregistré"));
    100. else{
    101.  
    102. QMessageBox::critical(0,tr("Erreur Insertion"),tr("Echec de L'enregistrement! <br> "
    103. "Vérifiez les valeurs.."));
    104. qDebug()<< model->lastError().text();
    105. }
    106. }
    107.  
    108.  
    109. }
    110.  
    111. Q_EXPORT_PLUGIN2(ITableManagerPlugin,ITableManager)
    To copy to clipboard, switch view to plain text mode 

    my ITableManager.pro

    Qt Code:
    1. QT += sql
    2.  
    3. TARGET = ITableManagerPlugin
    4. TEMPLATE = lib
    5.  
    6. DEFINES += ITABLEMANAGER_LIBRARY
    7.  
    8. SOURCES += itablemanager.cpp
    9.  
    10. INCLUDEPATH +=interface/
    11.  
    12. HEADERS += itablemanager.h\
    13. ITableManager_global.h\
    14.  
    15.  
    16. DESTDIR =../lib
    To copy to clipboard, switch view to plain text mode 

    so ,when i compile my program, i'm getting following errors in my application output pane

    debug\tablemanagerplugin.o:-1: In function `TableManagerInterface':
    TableManagerInterface.h:12: error: undefined reference to `vtable for TableManagerInterface'
    debug\tablemanagerplugin.o:-1: In function `TableManagerPlugin':
    tablemanagerplugin.h:8: error: undefined reference to `TableManagerInterface::~TableManagerInterface()'
    :-1: error: collect2: ld returned 1 exit status



    Someone can you tell me what the problem?
    Thanks in advance
    Last edited by maxoreli; 30th December 2011 at 01:41.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with my C++ library that uses a interface class

    tablemanagerplugin.h:8: error: ...

    where is this file?

    TableManagerInterface.h:12 <-- This is an empty line. Are you sure that what you have posted matches EXACTLY what is compiled by you?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with my C++ library that uses a interface class

    Qt Code:
    1. virtual void select(const QString &filter=0);
    To copy to clipboard, switch view to plain text mode 
    Method above is not virtual, I guess it's a mistake. Change it to pure virtual as below
    Qt Code:
    1. virtual void select(const QString &filter)=0
    To copy to clipboard, switch view to plain text mode 
    and the error will go away.

    Otherwise if you have an interface with at least one non-pure-virtual method move implementation of the destructor to a separate file (.cpp) and it will fix the error as well.

    Here's a link that will tell you more about it.

Similar Threads

  1. Replies: 5
    Last Post: 18th November 2011, 23:12
  2. ActiveX interface problem
    By skyperhh in forum Qt Programming
    Replies: 4
    Last Post: 9th January 2011, 22:46
  3. Problem with design interface
    By tux-world in forum Newbie
    Replies: 5
    Last Post: 10th March 2010, 14:19
  4. separate class using an interface
    By qt_gotcha in forum Newbie
    Replies: 7
    Last Post: 3rd March 2010, 21:54
  5. Using of Base Class Library(BCL)
    By jjbabu in forum Qt Programming
    Replies: 1
    Last Post: 24th October 2008, 17:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.