0
votes

I'm trying move my form with using left mouse button. I have this:

if(e->Button == Windows::Forms::MouseButtons::Left)
{
    Point^ mousePos = gcnew Point();
    mousePos = Control::MousePosition;
        mousePos->Offset(mouse_offset->X,mouse_offset->Y);
    Location = mousePos;
}

Code seems ok, but Visual returning error:

error C2664: 'void System::Windows::Forms::Control::Location::set(System::Drawing::Point)' : cannot convert parameter 1 from 'System::Drawing::Point ^' to 'System::Drawing::Point'

I don't understand - I created variable mousePos as a Point^. Do you have any idea what's wrong?

2
"I created variable mousePos as a Point^" - that's the problem. The method expects a Point, but you are giving it a pointer to Point.user529758
@H2CO3: you were right, thanks for help :)1_bug
There's a bigger problem when you write code like this, you are not understanding the difference between reference types (the ones you use the hat with) and value types, like Point. It matters a great deal, creating a Point^ like that creates a boxed value type. It is not a pointer to a Point, it is an object reference. Very inefficient and accepted by the compiler without complaint in too many cases. You got lucky by tripping a compile error here. Any decent book about C++/CLI will help you understand the difference.Hans Passant

2 Answers

1
votes

The problem is that the method expects a Point whereas you are passing it a Point ^ (managed pointer to Point - weeee, too much "point"s here...).

1
votes

Point is a value class (though not an immutable one). Why are you allocating one on the (managed) heap? I see nothing in this function that justifies doing that.