/*...*/
struct Point : public Object
{
sf::Vector2f coord;
int n;
Point();
Point(float x,float y);
Point operator=(QPoint pt);
};
class QSFMLCanvas
{ /*...*/ }
class MyCanvas : public QSFMLCanvas
{
Q_OBJECT //yes, this code involves Qt, in case the file names hadn't given it away yet
/*...*/
Point MouseCoords;
void ChangeView_Pan(QMouseEvent* e);
void mouseMoveEvent(QMouseEvent* e);
/*...*/
};
Here's the weird part, though. In MyCanvas.cpp, I have two calls to Point::operator=. The second one (MouseCoords = e->pos();) works like a charm. The first one (Point newcoords = e->pos();), however, throws the following error:
1>.\MyCanvas.cpp(32) : error C2440: 'initializing' : cannot convert from 'const QPoint' to 'Point'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
I know what that means, but it's clearly not the case since it's converting just fine on the second call (involving exactly the same variable type (QMouseEvent) and the same member function call (e->pos()).
When you declare a variable and assign to it, a copy constructor is called instead of assignment operator. If you write a copy constructor (Point(const QPoint&)) conversions will work everywhere. So I guess that's a better way.
your = operator is ill formed. It should return a reference and take a const reference:
1 2 3
// Point operator=(QPoint pt); // bad
Point& operator = (const QPoint& pt); // good
But your problem is because you're attempting to use a constructor and not the assignment operator. Since you seem to be using Point and QPoint interchangably, you should make a ctor that takes a QPoint:
1 2
Point(const QPoint& pt)
{ /*...*/ }
EDIT: doh, too slow.
and now that I think about it, it you just have the ctor, then you don't even need the assignment operator.
Thanks for the replies. This is the first time I've had to overload an operator, so I just copied the syntax from a book (Herbert Schildt's C++ A Beginner's Guide) which had it just as a put it, but everyone knows no Beginner's book is any good. :p
I'll make those alterations soon and see what happens.
I made the modifications and it worked, so thanks. However, I now have a related problem. I'm sure a similar solution needs to be done, but I can't figure it out...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//FaultLine.h
class Dialog : public QDialog
{
//...
private:
Dialog();
Dialog(QWidget* parent,Qt::WindowFlags f);
Dialog(const QWidget& parent,Qt::WindowFlags f);
};
class Canvas : public QWidget
{
//...
};
//Dialog.cpp
//...
#include "FaultLine.h"
Dialog::Dialog()
{
}
Dialog::Dialog(QWidget* parent=0, Qt::WindowFlags f=0) : QDialog(parent,f)
//QDialog belongs to the Qt library, included in the header file
{
this->setLayout(&Layout);
}
However, when I try to compile, I get a "cannot convert parameter 1 from 'Canvas *const ' to 'const Dialog &'" on the first line of Canvas::HorizonDialog(). I know I'm making a similar error, but I can't figure out what it is.
I figured as much, but then I don't know how to create a dynamic Dialog.
Dialog is meant to be constructed with two arguments: a QWidget* and a Qt::WindowFlags. Canvas inherits from QWidget and is thus a valid input for the Dialog constructor (right?). WindowFlags defaults to 0, so ignore it.
So, how can I make this single line of code work: Dialog* dlg = new Dialog(this /*Canvas object*/);
Personally, I don't even understand why it doesn't in the first place. This calls the constructor on the newly-allocated object, sending the pointer to a Canvas (i.e. QWidget) object to the constructor. Once all is said and done, it then sends a pointer to this object to dlg. Right?
EDIT: m4ster r0shi:
cannot convert parameter 1 from 'Canvas' to 'const Dialog &'
Yes, now that I look at it again I can't see any reason why it shouldn't work :/
I tried to recreate the problem here but failed... Does this here compile ok for you?
What I also find very odd is the fact that it seems to think I'm trying to convert dlg into a Canvas object (or the other way around).
cannot convert parameter 1 from 'Canvas' to 'const Dialog &'
I don't have any member functions with const Dialog & arguments. The only thing I can think of is the new function, which should return such a thing, but why is it misunderstanding the argument list and trying to return it instead of a pointer to the object containing those arguments?
Gargh.
It's also worth mentioning that if I use new Dialog();, it works just fine. It's the (Canvas)this pointer that's screwing it up.
I must have copied the class code wrong. The constructors and destructor are all public.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Dialog : public QDialog
{
Q_OBJECT
QString Value;
QHBoxLayout Layout;
public:
Dialog();
Dialog(QWidget* parent,Qt::WindowFlags f);
void AddToLayout(QWidget* widget);
public slots:
void ok_pressed();
signals:
void value(QString value);
};
Ignore the Q_OBJECT, slots and signals thing. It's some macro language belonging to the Qt library.
I've tried explicit casting. Just changes the error from
cannot convert parameter 1 from 'Canvas' to 'const Dialog &'
to
cannot convert parameter 1 from 'QWidget' to 'const Dialog &'
And this function is a call to open a modeless dialog in an interface (i.e. a dialog that immediately returns control to the caller function, without stopping the process while open). This means that once the function returns, the objects would be destroyed if they weren't dynamically allocated. That was a mistake I made before I got this error. :p
Actually, I figured it out. I still don't understand the error message or why this was a problem, but I noticed that all the arguments on the constructor had default values. Going on a completely random guess, I thought that might somehow be a problem, perhaps confusing the compiler between the default constructor and a constructor with all default values.
So I took out the default value to the first argument and it compiled!