Results 1 to 9 of 9

Thread: cannot convert 'this' pointer from 'const Square' to 'Square &'

  1. #1
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default cannot convert 'this' pointer from 'const Square' to 'Square &'

    Hi, I'm trying to get up a class for a Qt program, but it gives me this errors:
    Error 1 error C2662: 'Square::left' : cannot convert 'this' pointer from 'const Square' to 'Square &' c:\Chus\konstructor\src\Square.cpp 6
    Error 2 error C2662: 'Square::up' : cannot convert 'this' pointer from 'const Square' to 'Square &' c:\Chus\konstructor\src\Square.cpp 6
    Error 3 error C2662: 'Square::right' : cannot convert 'this' pointer from 'const Square' to 'Square &' c:\Chus\konstructor\src\Square.cpp 6
    Error 4 error C2662: 'Square::down' : cannot convert 'this' pointer from 'const Square' to 'Square &' c:\Chus\konstructor\src\Square.cpp 6
    The class code is:
    Square.h:
    Qt Code:
    1. #pragma once
    2.  
    3. #include <QPolygon>
    4.  
    5. class Square:public QPolygon{
    6. public:
    7. Square();
    8. Square(const Square &rhs);
    9. Square(const QPoint left,const QPoint up,const QPoint right,const QPoint down);
    10.  
    11. QPoint left(){return data()[left];}
    12. QPoint up(){return data()[UP];}
    13. QPoint right(){return data()[right];}
    14. QPoint down(){return data()[DOWN];}
    15. void setLeft(QPoint left);
    16. void setUp(QPoint up);
    17. void setRight(QPoint right);
    18. void setDown(QPoint down);
    19. private:
    20. enum Points {LEFT=0,UP=1,RIGHT=2,DOWN=3};
    21. };
    To copy to clipboard, switch view to plain text mode 
    Square.cpp:
    Qt Code:
    1. #include "Square.h"
    2.  
    3. Square::Square():QPolygon(4){}
    4.  
    5. Square::Square(const Square &rhs){
    6. Square(rhs.left(),rhs.up(),rhs.right(),rhs.down());
    7. }
    8.  
    9. Square::Square(const QPoint left,const QPoint up,const QPoint right,const QPoint down){
    10. // Añadir los puntos dados al constructor
    11. setPoints(4, // Número de puntos del polÃ*gono
    12. left.x(),left.y(), // Izquierda
    13. up.x(),up.y(), // Arriba
    14. right.x(),right.y(), // Derecha
    15. down.x(),down.y() // Abajo
    16. );
    17. }
    18.  
    19. void Square::setLeft(QPoint left){
    20. setPoint(LEFT,left);
    21. }
    22.  
    23. void Square::setUp(QPoint up){
    24. setPoint(UP,up);
    25. }
    26.  
    27. void Square::setRight(QPoint right){
    28. setPoint(RIGHT,right);
    29. }
    30.  
    31. void Square::setDown(QPoint down){
    32. setPoint(DOWN,down);
    33. }
    To copy to clipboard, switch view to plain text mode 
    Thank's for the help

    NOTE: In lines 11, 13 appears [left] and [right] it's [L EFT] and [R IGHT] in my code. I don't know why it converts to lowercase on copy&paste.

  2. #2
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot convert 'this' pointer from 'const Square' to 'Square &'

    I've solved it changin the line

    Square(rhs.left(),rhs.up(),rhs.right(),rhs.down());
    Square(rhs.data()[left],rhs.data()[UP],rhs.data()[right],rhs.data()[DOWN]);

    But I would want to know why the other line fails.

  3. #3
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: cannot convert 'this' pointer from 'const Square' to 'Square &'

    Are you sure your constructor should not look like this:

    #
    Square(const QPoint &left,const QPoint &up,const QPoint &right,const QPoint &down);
    #

  4. #4
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot convert 'this' pointer from 'const Square' to 'Square &'

    Same result :s

  5. #5
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: cannot convert 'this' pointer from 'const Square' to 'Square &'

    Ok, just realised, this should not work regardless what you do:

    Square::Square(const Square &rhs){
    Square(rhs.left(),rhs.up(),rhs.right(),rhs.down()) ;
    }

    As far as I think to know, you cannot call a constructor within a constructor in C++. This would be JAVA

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot convert 'this' pointer from 'const Square' to 'Square &'

    It should be:
    Qt Code:
    1. QPoint left() const { return data()[left]; }
    2. QPoint up() const { return data()[UP]; }
    3. QPoint right() const { return data()[right]; }
    4. QPoint down() const { return data()[DOWN]; }
    To copy to clipboard, switch view to plain text mode 

    http://www.parashift.com/c++-faq-lit...rrectness.html

  7. #7
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: cannot convert 'this' pointer from 'const Square' to 'Square &'

    This surely will make his program compile, but will his copy constructor work as expected? As far as I know you cannot call a constructor directly.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot convert 'this' pointer from 'const Square' to 'Square &'

    Quote Originally Posted by Kumosan
    This surely will make his program compile, but will his copy constructor work as expected? As far as I know you cannot call a constructor directly.
    No, it won't work, but you already wrote that, didn't you?

  9. #9
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: cannot convert 'this' pointer from 'const Square' to 'Square &'

    Quote Originally Posted by jacek
    No, it won't work, but you already wrote that, didn't you?
    Yep. did. But with those more 'acrane' rules of C++ I am never sure. And since you just corrected his code that much that it compiles without warning him again to fix his constructor, I just got more unsure.

Similar Threads

  1. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  2. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 19:47
  3. Replies: 10
    Last Post: 10th February 2006, 00:15

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.