PDA

View Full Version : cannot convert 'this' pointer from 'const Square' to 'Square &'



xgoan
2nd August 2006, 10:53
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:

#pragma once

#include <QPolygon>

class Square:public QPolygon{
public:
Square();
Square(const Square &rhs);
Square(const QPoint left,const QPoint up,const QPoint right,const QPoint down);

QPoint left(){return data()[left];}
QPoint up(){return data()[UP];}
QPoint right(){return data()[right];}
QPoint down(){return data()[DOWN];}
void setLeft(QPoint left);
void setUp(QPoint up);
void setRight(QPoint right);
void setDown(QPoint down);
private:
enum Points {LEFT=0,UP=1,RIGHT=2,DOWN=3};
};
Square.cpp:

#include "Square.h"

Square::Square():QPolygon(4){}

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

Square::Square(const QPoint left,const QPoint up,const QPoint right,const QPoint down){
// Añadir los puntos dados al constructor
setPoints(4, // Número de puntos del polÃ*gono
left.x(),left.y(), // Izquierda
up.x(),up.y(), // Arriba
right.x(),right.y(), // Derecha
down.x(),down.y() // Abajo
);
}

void Square::setLeft(QPoint left){
setPoint(LEFT,left);
}

void Square::setUp(QPoint up){
setPoint(UP,up);
}

void Square::setRight(QPoint right){
setPoint(RIGHT,right);
}

void Square::setDown(QPoint down){
setPoint(DOWN,down);
}
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.

xgoan
2nd August 2006, 11:02
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.

Kumosan
2nd August 2006, 11:55
Are you sure your constructor should not look like this:

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

xgoan
2nd August 2006, 12:46
Same result :s

Kumosan
2nd August 2006, 13:18
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 ;)

jacek
2nd August 2006, 16:15
It should be:
QPoint left() const { return data()[left]; }
QPoint up() const { return data()[UP]; }
QPoint right() const { return data()[right]; }
QPoint down() const { return data()[DOWN]; }

http://www.parashift.com/c++-faq-lite/const-correctness.html

Kumosan
2nd August 2006, 16:49
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.

jacek
2nd August 2006, 17:01
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?

Kumosan
2nd August 2006, 17:19
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.