Results 1 to 8 of 8

Thread: Linker does not accept overloaded "operator>>"

  1. #1
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Linker does not accept overloaded "operator>>"

    Hi,
    I've got a linker error; that is, it does not accept this expression:
    Qt Code:
    1. stream << my_object.field
    To copy to clipboard, switch view to plain text mode 
    where the field is of qint16 type.

    Generally, the program is supposed to test whether there is a file called "database" in the same folder as the application. If it does not exists, it is created. If it does exist, then the application connects to a database and fetch the date and time of fields modification. If the datetime of a file is earlier then the database, then the file on HDD is overwritten with new data. I have two problems here. The linker does not accept overloaded operators << and >>:
    Qt Code:
    1. D:\Documents\Programming C++\Qt\wydarzenia\stworz_plik\stworz_plik\rekord.cpp:20: error: no match for 'operator>>' in 'stream >> r.Rekord::przypomnienie'
    To copy to clipboard, switch view to plain text mode 
    When I tried to overload operators << and >> for qint16, although it looked strange I'd have to do so, I've got segmentation error. I suspect it is due to line 33 in file "definicje_globalne.h" where data are sent to a stream associated with a file on HDD. I cannot find the cause of error. I hope providing all codes will make understand what I am trying to do and help me with the error. I will be grateful for any help and comments.

    Thanks



    plik.pro
    Qt Code:
    1. SOURCES += \
    2. main.cpp \
    3. rekord.cpp
    4.  
    5. HEADERS += \
    6. definicje_globalne.h \
    7. rekord.h
    8.  
    9. QT += sql
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include "rekord.h"
    4. #include "definicje_globalne.h"
    5.  
    6. int main(int argc, char* argv[]) {
    7. QApplication app(argc,argv);
    8.  
    9. QString fileName = "database";
    10. QFile dbFile(fileName);
    11. if(dbFile.exists()) {
    12. qDebug() << "Plik database istnieje na dysku"; // "The file database exists"
    13. bool isUpdated;
    14. testUpdate(fileName,isUpdated);
    15. if(!isUpdated) {
    16. qDebug() << "Plik database jest nieuaktualny"; // "The file database is not up to date"
    17. if(createFile(dbFile)) {
    18. qDebug() << "Plik database zostal stworzony";
    19. } else {
    20. qDebug() << "Wystapil blad w tworzeniu poliku database";
    21. }
    22. } else {
    23. qDebug() << "Plik database jest aktualny"; // "The file database is up to date"
    24. } // if(!isUpdated)
    25. } else {
    26. qDebug() << "Plik database nie istnieje na dysku. Tworzenie pliku database."; // "The file database is not found. The file is being created ..."
    27. createFile(dbFile);
    28. }
    29. return app.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

    rekord.h
    Qt Code:
    1. #ifndef REKORD_H
    2. #define REKORD_H
    3. #include <QString>
    4. #include <QDate>
    5. #include <QtGlobal>
    6.  
    7. struct Rekord {
    8. QString imie; // name
    9. QString nazwisko; // surname
    10. QString nazwa_wyd; // event name
    11. QDateTime data_wyd; // event date
    12. qint16 przypomnienie; // remainder
    13.  
    14. Rekord(
    15. const QString& im = "",
    16. const QString& na = "",
    17. const QString& nw = "",
    18. const QDateTime& dw = QDateTime::currentDateTime(),
    19. const qint16& p = 0
    20. );
    21. };
    22.  
    23. QDataStream& operator<<(QDataStream& stream, const Rekord& r);
    24. QDataStream& operator>>(QDataStream& stream, Rekord& r);
    25. //QDataStream& operator<<(QDataStream& stream, const qint16& qi);
    26. //QDataStream& operator>>(QDataStream& stream, const qint16& qi);
    27.  
    28. #endif // REKORD_H
    To copy to clipboard, switch view to plain text mode 

    rekord.cpp
    Qt Code:
    1. #include "rekord.h"
    2.  
    3. Rekord::Rekord(const QString& im, const QString& na, const QString& nw, const QDateTime &dw, const qint16& p)
    4. : imie(im),nazwisko(na),nazwa_wyd(nw),data_wyd(dw),przypomnienie(p) {}
    5.  
    6. QDataStream& operator<<(QDataStream& stream, const Rekord& r) {
    7. stream << r.imie;
    8. stream << r.nazwisko;
    9. stream << r.nazwa_wyd;
    10. stream << r.data_wyd;
    11. stream << r.przypomnienie;
    12. return stream;
    13. }
    14.  
    15. QDataStream& operator>>(QDataStream& stream, Rekord& r) {
    16. stream >> r.imie;
    17. stream >> r.nazwisko;
    18. stream >> r.nazwa_wyd;
    19. stream >> r.data_wyd;
    20. stream >> r.przypomnienie;
    21. return stream;
    22. }
    To copy to clipboard, switch view to plain text mode 

    definicje_globalne.h
    Qt Code:
    1. #ifndef DEFINICJE_GLOBALNE_H
    2. #define DEFINICJE_GLOBALNE_H
    3. #include <QtGui>
    4. #include <QtSql>
    5. #include "rekord.h"
    6.  
    7. bool createFile(QFile& dbFile) {
    8. bool flag = true;
    9. if(dbFile.open(QIODevice::WriteOnly)) {
    10. QSqlDatabase dbRead = QSqlDatabase::addDatabase("QMYSQL");
    11. dbRead.setHostName("localhost");
    12. dbRead.setDatabaseName("wydarzenia");
    13. dbRead.setUserName("user");
    14. dbRead.setPassword("password");
    15. bool dbReadOk = dbRead.open();
    16. if(dbReadOk) {
    17. qDebug() << "Baza danych wydarzenia otwarta"; // The database wydarzenia is open
    18. QList<Rekord> dbStore;
    19. QSqlQuery dbReadQr("SELECT osoby.imie,osoby.nazwisko,nazwa_wydarzenia.nazwa,wydarzenia.data_wyd,wydarzenia.przypomnienie FROM osoby,nazwa_wydarzenia,wydarzenia WHERE wydarzenia.osoba=osoby.ID AND wydarzenia.wydarzenie=nazwa_wydarzenia.ID;");
    20. if(dbReadQr.isActive()) {
    21. qDebug() << "Zapytanie do bazy wydarzenia OK"; // "The query to database wydarzenia is active"
    22. while(dbReadQr.next()) {
    23. Rekord dbRekord(
    24. dbReadQr.value(0).toString(),
    25. dbReadQr.value(1).toString(),
    26. dbReadQr.value(2).toString(),
    27. dbReadQr.value(3).toDateTime(),
    28. static_cast<qint16>(dbReadQr.value(4).toInt())
    29. );
    30. dbStore.append(dbRekord);
    31. }
    32. QDataStream stream(&dbFile);
    33. stream << dbStore;
    34. } else {
    35. qDebug() << "Blad zapytania do bazy danych wydarzenia"; // Cannot send a query to the database wydarzenia
    36. flag = false;
    37. } // if(dbReadQr.isActive())
    38. dbRead.close();
    39. qDebug() << "Baza danych wydarzenia zamknieta"; // The database wydarzenia is closed
    40. } else {
    41. qDebug() << "Blad otwarcia bazy wydarzenia"; // Cannot open the database wydarzenia
    42. flag = false;
    43. } // if(dbReadOk)
    44. dbFile.close();
    45. qDebug() << "Plik database zamkniety"; // The file database is closed
    46. } else {
    47. qDebug() << "Blad otwarcia pliku database"; // Cannot open the file database
    48. qDebug() << dbFile.errorString();
    49. flag = false;
    50. } // if(dbFile.open(QIODevice::WriteOnly))
    51. return flag;
    52. }
    53.  
    54. bool testUpdate(const QString& fileName, bool& isUpdated) {
    55. bool flag = true;
    56. QFileInfo dbInfo(fileName);
    57. QSqlDatabase dbStatus = QSqlDatabase::addDatabase("QMYSQL");
    58. dbStatus.setHostName("localhost");
    59. dbStatus.setDatabaseName("wydarzenia_status");
    60. dbStatus.setUserName("user");
    61. dbStatus.setPassword("password");
    62. bool dbStatusOk = dbStatus.open();
    63. if(dbStatusOk) {
    64. qDebug() << "Baza danych wydarzenia_status otwarta."; // The database wydarzenia_status is open
    65. QSqlQuery dbStatusQr("SELECT name,datetime FROM tables;");
    66. if(dbStatusQr.isActive()) {
    67. qDebug() << "Zapytanie do bazy wydarzenia_status OK"; // Active query to database wydarzenia_status
    68. while(dbStatusQr.next()) {
    69. dbInfo.lastModified() >= dbStatusQr.value(1).toDateTime() ? isUpdated = true : isUpdated = false;
    70. }
    71. } else {
    72. qDebug() << "Blad zapytania do bazy danych wydarzenia_status"; // Cannot send a query to the database wydarzenia_status
    73. flag = false;
    74. } // if(dbStatusQr.isActive())
    75. } else {
    76. qDebug() << "Blad otwarcia bazy danych wydarzenia_status"; // Cannot open the database wydarzenia_status
    77. flag = false;
    78. } // if(dbStatusOk)
    79. dbStatus.close();
    80. qDebug() << "Baza danych wydarzenia_status zamknieta"; // "The database <wyd...> is closed"
    81. return flag;
    82. }
    83.  
    84. #endif // DEFINICJE_GLOBALNE_H
    To copy to clipboard, switch view to plain text mode 


    Added after 12 minutes:
    Last edited by ZikO; 30th August 2012 at 20:32.

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

    Default Re: Linker does not accept overloaded "operator>>"

    please only try to tackle one problem in a thread, otherwise there will be lots of cross-talk. Do you want to discuss seg fault issue or compiler issue?
    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
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Linker does not accept overloaded "operator>>"

    Hi,

    Well. Both are important but I would really like to understand what I did wrong that compiler reports error.

    Thnks for answer.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Linker does not accept overloaded "operator>>"

    At the top of rekord.cpp:
    Qt Code:
    1. #include <QDataStream>
    To copy to clipboard, switch view to plain text mode 

    rekord.h only includes a forward declaration of QDataStream (through QtGlobal) that is enough to support the declarations you make there.

  5. The following user says thank you to ChrisW67 for this useful post:

    ZikO (31st August 2012)

  6. #5
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Linker does not accept overloaded "operator>>"

    Quote Originally Posted by ChrisW67 View Post
    At the top of rekord.cpp:
    Qt Code:
    1. #include <QDataStream>
    To copy to clipboard, switch view to plain text mode 

    rekord.h only includes a forward declaration of QDataStream (through QtGlobal) that is enough to support the declarations you make there.
    Hi Chris,
    Thanks for your help. I cannot believe I've missed that line -.- I feel awkward.
    I included it as you have specified but I am sorry I did not really understand what you were saying with regard to rekord.h. and QGlobal. Though, I assumed I needed to put this line
    Qt Code:
    1. #include <QDataStream>
    To copy to clipboard, switch view to plain text mode 
    in rekord.cpp. because, apart from declarations in QGlobal, I would also need definitions from QDataStream.

    Anyway, the application's working

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Linker does not accept overloaded "operator>>"

    When compiling rekord.cpp the compiler first includes rekord.h and then processes the remainder of the cpp file. Your declaration of the two Rekord stream operators in the header requires only a forward declaration of QDataStream because they are only references (or pointers) to a QDataStream object. In your case, perhaps by accident rather than design, this is provided by the QtGlobal header you include. By the time the compiler gets to your definition of Rekord streaming operators the compiler needs a full prototype for QDataStream and its streaming functions and does not have it. The streaming operators for QString and QDateTime are in the headers for those classes, so they compiled fine. The prototype for the qint16 streaming operator is provided by the QDataStream header.

  8. #7
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Linker does not accept overloaded "operator>>"

    Hi Chris,

    Thanks for this great piece of knowledge . Now, I understand what has happened. I did not come acros the "forward declaration" term before and as I googled it I can follow your explanation now. QGlobal was not included by accident. I thought it was needed by qint16 and any other platform independent types. I found it in Qt Documentation.

  9. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Linker does not accept overloaded "operator>>"

    QtGlobal includes a typedef for qint16 either directly or indirectly (I don't have Qt on this iPad to check). I suspect that including almost any Qt header indirectly includes QtGlobal anyway.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. Qt Creator How to add "Additional Dependencies" (Linker settings)
    By tRs in forum Qt Tools
    Replies: 1
    Last Post: 8th December 2011, 19:04
  2. Replies: 3
    Last Post: 22nd February 2011, 09:53
  3. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  4. On MacOSX, QMessage don't accept "\n"
    By manhds in forum Qt Programming
    Replies: 7
    Last Post: 7th June 2006, 16:04

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.