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:
Quote:
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:
Code:
#pragma once
#include <QPolygon>
public:
Square();
Square(const Square &rhs);
QPoint left
(){return data
()[left
];
} QPoint up
(){return data
()[UP
];
} QPoint right
(){return data
()[right
];
} QPoint down
(){return data
()[DOWN
];
} private:
enum Points {LEFT=0,UP=1,RIGHT=2,DOWN=3};
};
Square.cpp:
Code:
#include "Square.h"
Square::Square(const Square &rhs){
Square(rhs.left(),rhs.up(),rhs.right(),rhs.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.
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.
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);
#
Re: cannot convert 'this' pointer from 'const Square' to 'Square &'
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 ;)
Re: cannot convert 'this' pointer from 'const Square' to 'Square &'
It should be:
Code:
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-lit...rrectness.html
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.
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?
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.