operator= issue

Before asking the question, I think it better to show the relevant code snippets.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//MyCanvas.cpp
#include "Qt.h"
/*...*/
void MyCanvas::ChangeView_Pan(QMouseEvent* e)
{
   /*...*/
   Point newcoords = e->pos();
   /*...*/
}
/*...*/
void MyCanvas::mouseMoveEvent(QMouseEvent* e)
{
   if(Drag)
      ChangeView_Pan(e);
   MouseCoords = e->pos();
}


1
2
3
4
5
6
7
8
9
//Qt.cpp
/*...*/
Point Point::operator=(QPoint pt)
{
   coord.x=pt.x();
   coord.y=pt.y();
   return *this;
}
/*...*/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*...*/
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()).

Anyone know what I'm doing blatantly wrong?
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.
Last edited on
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
{
        //...
};


1
2
3
4
5
6
7
8
9
10
//Canvas.cpp
#include "FaultLine.h"

//...
void Canvas::HorizonDialog()
{
	Dialog* dlg = new Dialog(this);
        //...
}
//... 


1
2
3
4
5
6
7
8
9
10
11
12
//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.
Last edited on
*bump*
when using a copy constructor the object you pass in must be of the same type as the object you're creating.

In Canvas::HorizonDialog() you create a Dialog object but pass in the 'this' pointer which in this case is a Canvas object.
Try this -> Dialog* dlg = new Dialog(*this);
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 &'

:S
Last edited on
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

class BaseB;

class BaseA {public: virtual ~BaseA(){}};
class DerivedA: public BaseA
{
public:
    DerivedA(){}
    DerivedA(const BaseB*,int a=0)
    {
        cout << "cool!" << endl;
    }
};

class BaseB {public: virtual ~BaseB(){}};
class DerivedB: public BaseB
{
public:
    DerivedB(){}
    void Asdf()
    {
        DerivedA * da=new DerivedA(this);
        delete da;
    }
};

int main()
{
    DerivedB db;
    db.Asdf();

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}
Compiles just fine.

Gargh.

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.
Any other suggestions? I could really use a solution for this one...
I can't understand how is that new Dialog(); works.
Dialog has only private constructors, so how is that Canvas can see them?

Try to explicit cast the pointer

Also, is there a reason that you couldn't do Dialog dlg(this); instead of using pointers

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!

Dialog(QWidget* parent,Qt::WindowFlags f=0);
Topic archived. No new replies allowed.