Results 1 to 7 of 7

Thread: class operator problem

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default class operator problem

    Hi to all!

    I have following chunk of code:
    Qt Code:
    1. bool CMerchandizeOrder::operator==(const CMerchandizeOrder& comparedOrder) const
    2. {
    3. bool flag1=false;
    4. bool flag2=false;
    5. bool flag3=false;
    6. bool flag4=false;
    7. // bool flag5=false;
    8. bool flag6=false;
    9.  
    10. if(merchandizeID()==comparedOrder.merchandizeID())
    11. flag1=true;
    12. if(merchandizeQuantity()==comparedOrder.merchandizeQuantity())
    13. flag2=true;
    14. if(merchandizePrice()==comparedOrder.merchandizePrice())
    15. flag3=true;
    16. if(merchandizeSubtotal()==comparedOrder.merchandizeSubtotal())
    17. flag4=true;
    18. /*
    19.   if(this->m_strDisplayString==comparedOrder.merchandizeDisplayString())
    20.   flag5=true;
    21. */
    22. if(merchandizeName()==comparedOrder.merchandizeName())
    23. flag6=true;
    24. if(flag1==true && flag2==true && flag3==true && flag4==true /*&& flag5==true*/ && flag6==true)
    25. return true;
    26. return false;
    27. }
    To copy to clipboard, switch view to plain text mode 
    When i try to compile it i get following errors:
    Qt Code:
    1. mingw32-make
    2. mingw32-make -f Makefile.Debug
    3. mingw32-make[1]: Entering directory `C:/Documents and Settings/markofr/workspace/Client'
    4. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.4.0\include\QtCore" -I"..\..\..\..\Qt\4.4.0\include\QtCore" -I"..\..\..\..\Qt\4.4.0\include\QtGui" -I"..\..\..\..\Qt\4.4.0\include\QtGui" -I"..\..\..\..\Qt\4.4.0\include\QtSql" -I"..\..\..\..\Qt\4.4.0\include\QtSql" -I"..\..\..\..\Qt\4.4.0\include" -I"c:\Qt\4.4.0\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.4.0\mkspecs\win32-g++" -o debug\CMerchandizeOrder.o CMerchandizeOrder.cpp
    5. CMerchandizeOrder.cpp: In member function `bool CMerchandizeOrder::operator==(const CMerchandizeOrder&) const':
    6. CMerchandizeOrder.cpp:32: error: passing `const CMerchandizeOrder' as `this' argument of `const qint16 CMerchandizeOrder::merchandizeID()' discards qualifiers
    7. CMerchandizeOrder.cpp:32: error: passing `const CMerchandizeOrder' as `this' argument of `const qint16 CMerchandizeOrder::merchandizeID()' discards qualifiers
    8. CMerchandizeOrder.cpp:34: error: passing `const CMerchandizeOrder' as `this' argument of `const qint16 CMerchandizeOrder::merchandizeQuantity()' discards qualifiers
    9. CMerchandizeOrder.cpp:34: error: passing `const CMerchandizeOrder' as `this' argument of `const qint16 CMerchandizeOrder::merchandizeQuantity()' discards qualifiers
    10. CMerchandizeOrder.cpp:36: error: passing `const CMerchandizeOrder' as `this' argument of `const qreal CMerchandizeOrder::merchandizePrice()' discards qualifiers
    11. CMerchandizeOrder.cpp:36: error: passing `const CMerchandizeOrder' as `this' argument of `const qreal CMerchandizeOrder::merchandizePrice()' discards qualifiers
    12. CMerchandizeOrder.cpp:38: error: passing `const CMerchandizeOrder' as `this' argument of `const qreal CMerchandizeOrder::merchandizeSubtotal()' discards qualifiers
    13. CMerchandizeOrder.cpp:38: error: passing `const CMerchandizeOrder' as `this' argument of `const qreal CMerchandizeOrder::merchandizeSubtotal()' discards qualifiers
    14. CMerchandizeOrder.cpp:44: error: passing `const CMerchandizeOrder' as `this' argument of `const QString CMerchandizeOrder::merchandizeName()' discards qualifiers
    15. CMerchandizeOrder.cpp:44: error: passing `const CMerchandizeOrder' as `this' argument of `const QString CMerchandizeOrder::merchandizeName()' discards qualifiers
    16. mingw32-make[1]: *** [debug/CMerchandizeOrder.o] Error 1
    17. mingw32-make[1]: Leaving directory `C:/Documents and Settings/markofr/workspace/Client'
    18. mingw32-make: *** [debug] Error 2
    To copy to clipboard, switch view to plain text mode 

    What does that mean and how do I get rid of these errors?!
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: class operator problem

    You are calling non-const methods in a const method (and via const reference). CMerchandizeOrder::merchandizeID() etc. should be const methods just like the operator is.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: class operator problem

    But they are const:
    Qt Code:
    1. inline const qint16 merchandizeID()
    2. { return m_OrderValues.iMerchandizeID; };
    3. inline void setMerchandizeID(qint16 iMerchandizeID)
    4. { m_OrderValues.iMerchandizeID=iMerchandizeID; };
    5.  
    6. inline const QString merchandizeName()
    7. { return m_OrderValues.strMerchandizeName; };
    8. inline void setMerchandizeName(QString& strName)
    9. { m_OrderValues.strMerchandizeName=strName; };
    10.  
    11. inline const qreal merchandizePrice()
    12. { return m_OrderValues.rMerchandizePrice; };
    13. inline void setMerchandizePrice(qreal rPrice)
    14. { m_OrderValues.rMerchandizePrice=rPrice; };
    15.  
    16. inline const qint16 merchandizeQuantity()
    17. { return m_OrderValues.iMerchandizeQuantity; };
    18. inline void setMerchandizeQuantity(qint16 iQuantity)
    19. { m_OrderValues.iMerchandizeQuantity=iQuantity; };
    20.  
    21. inline const qreal merchandizeSubtotal()
    22. { return m_OrderValues.rSubtotal; };
    23. inline void setSubtotal(qreal rSubtotal)
    24. { m_OrderValues.rSubtotal=rSubtotal; };
    To copy to clipboard, switch view to plain text mode 

    I simply do not get it ...
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: class operator problem

    They return const values but the methods itself are not const.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: class operator problem

    How do I make them const? If I write for instance:
    Qt Code:
    1. inline const qint16 merchandizeID()
    2. { return m_OrderValues.iMerchandizeID; } const;
    To copy to clipboard, switch view to plain text mode 
    I get this error:
    Qt Code:
    1. mingw32-make
    2. mingw32-make -f Makefile.Debug
    3. mingw32-make[1]: Entering directory `C:/Documents and Settings/markofr/workspace/Client'
    4. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.4.0\include\QtCore" -I"..\..\..\..\Qt\4.4.0\include\QtCore" -I"..\..\..\..\Qt\4.4.0\include\QtGui" -I"..\..\..\..\Qt\4.4.0\include\QtGui" -I"..\..\..\..\Qt\4.4.0\include\QtSql" -I"..\..\..\..\Qt\4.4.0\include\QtSql" -I"..\..\..\..\Qt\4.4.0\include" -I"c:\Qt\4.4.0\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.4.0\mkspecs\win32-g++" -o debug\CShoppingCartModel.o CShoppingCartModel.cpp
    5. In file included from CShoppingCartModel.h:9,
    6. from CShoppingCartModel.cpp:1:
    7. CMerchandizeOrder.h:20: error: declaration does not declare anything
    8. mingw32-make[1]: *** [debug/CShoppingCartModel.o] Error 1
    9. mingw32-make[1]: Leaving directory `C:/Documents and Settings/markofr/workspace/Client'
    10. mingw32-make: *** [debug] Error 2
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: class operator problem

    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: class operator problem

    Qt Code:
    1. inline const qint16 merchandizeID() const
    2. { return m_OrderValues.iMerchandizeID; }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  2. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 07:07
  3. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:05
  4. Replies: 2
    Last Post: 16th March 2007, 09:04
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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.