POINT error

Hey,

What's wrong with this code?

1
2
case WM_CREATE:
ball = new POINT((window.right - window.left) / 2, (window.bottom - window.top) / 2);


Error:
error C2661: 'tagPOINT::tagPOINT' : no overloaded function takes 2 arguments

According to tutorials, it should take two arguments. What am I doing wrong?

Thanks
We need more code than that to diagnose the problem.
I think this can solve the problem...
1
2
3
4
5
6
...
	case WM_CREATE: 
		ball = new POINT;
		ball->x = window.right - window.left/ 2;
		ball->y = window.bottom - window.top / 2;
....

Hey,

Thank you, that indeed solved the problem. But why? what was wrong with my code?

Thanks

EDIT:
If I want to display a data member of the class in a messagebox, I need to convert it to a C-string. I thought this would be possible:

MessageBox(hWnd, (ball->x).c_str(), "Test", MB_OK);

But it seems it's not. Error:
error C2228: left of '.c_str' must have class/struct/union

What's the correct way to do this?
Last edited on
As the error is telling You, x is not an class or struct on which You are able to use the "."-operator...

c_str() is only a member of the std::string-type, so you need to pass your x and y-values into an string, or character first... (You need to convert the integer into an character)...

http://cplusplus.com/articles/numb_to_text/ <- this could be very useful...

Create an stringstream object, pass in the values (dont forget an delimiter between them) and then return the string from it, on which you may call .c_str() instanly, too...

1
2
3
stringstream ss;
//get the c_str() out of ss
ss.str().c_str();
Last edited on
Hey,

Thank you - that works fine.

Any idea on the first problem? I really don't understand why W Neto's code works, but mine doesn't.

I'm trying to create a very simple pong game, step by step.

Thanks
this is, because there is not constructor for a tagPOINT, which takes two arguments (in Your case the x and y coordinates)...

you could do POINT P = {0,0};

But that is no dynamic allocation...
Hey,

I don't really understand this...

POINT P = {0,0};

This isn't a constructor, but what is it?
Hey

I'm stuck again with this...

I'm now trying to make it a bit more OOP, so I created this class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef BALL_H
#define BALL_H

#include <windows.h>

class Ball {
public:
	Ball(POINT position, POINT speed, int height, RECT client);

	void update();
	void draw(HDC bbDC);

private:
	POINT position;
	POINT speed;
	int height;
	RECT client;
};

#endif 


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
#include "Ball.h"

Ball::Ball(POINT position, POINT speed, int height, RECT client) 
{
	this->position = position;
	this->speed = speed;
	this->height = height;
	this->client = client;

}

void Ball::update() 
{
	position.x += speed.x;
	position.y += speed.y;

	if (position.x + 12 > client.right - client.left || position.x - 12 < 0)
		speed.x *= -1;
			
	if (position.y + 12 > client.bottom - client.top || position.y - 12 < 0)
		speed.y *= -1;
}

void Ball::draw(HDC bbDC)
{
	Ellipse(bbDC, position.x - height, position.y - height, position.x + height, position.y + height);
}


(For now, the project just has a ball bouncing at the walls - nothing more).

In the WM_CREATE message, I would like to make the ball object, passing in the speed and position (see the constructor). In C# it's possible to create this in the parameter, like this:

player = new player(new Point(75, 75));

Is this possible in my situation, in C++? It avoids creating a lot of objects which are only used for the constructor.

Thanks!
Last edited on
It is certainly possible, except you didn't define POINT as having a constructor that takes 2 arguments. Also, new returns a pointer to an object, not an object.
he ist using the tagPoint structure from the MFC... which has not the corresponding constructor for doing it...

You could create your own Point class with an appropriate constructor...

EDIT: http://codepad.org/ZmEKjoC6
Last edited on
Hey,

I've created my own Point class, but it's not fully working. I think I did it correctly with the pointers, but I'm not sure...

1
2
3
4
5
6
7
8
9
Ball*		ball		= 0;

....


case WM_CREATE:
		GetClientRect(hWnd, &client);

		ball = new Ball(new Point((client.right - client.left) / 2, (client.bottom - client.top) / 2), new Point(2, 2), 13, client);


ball.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef BALL_H
#define BALL_H

#include <windows.h>
#include "Point.h"

class Ball {
public:
	Ball(Point* position, Point* speed, int height, RECT &client);

	void update();
	void draw(HDC bbDC);

private:
	Point position;
	Point speed;
	int height;
	RECT client;
};

#endif 


ball.cpp

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
#include "Ball.h"
#include "Point.h"

Ball::Ball(Point* position, Point* speed, int height, RECT &client) 
{
	this->position = *position;
	this->speed = *speed;
	this->height = height;
	this->client = client;

}

void Ball::update() 
{
	position.x += speed.x;
	position.y += speed.y;

	if (position.x + 12 > client.right - client.left || position.x - 12 < 0)
		speed.x *= -1;
			
	if (position.y + 12 > client.bottom - client.top || position.y - 12 < 0)
		speed.y *= -1;
}

void Ball::draw(HDC bbDC)
{
	Ellipse(bbDC, position.x - height, position.y - height, position.x + height, position.y + height);
}


point.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef POINT_H
#define POINT_H

#include <windows.h>

class Point {
public:
	Point(int x, int y);

	int x;
	int y;
};

#endif 


point.cpp

1
2
3
4
5
6
7
#include "Point.h"

Point::Point(int x, int y) 
{
	this->x = x;
	this->y = y;
}


ball.cpp(5) : error C2512: 'Point' : no appropriate default constructor available
(this error occurs two times).

Any idea what's wrong?

Thanks!
you are missing a default constructor.

1
2
3
class Point{
  Point(){}
}
Thanks, that solved the problem.

But why is a default constructor needed here? I never had to use one before. I've looked it up in the tutorial (http://cplusplus.com/doc/tutorial/classes/) but still don't understand why we need one in this case.

Thanks
When you specify your own constructor the complier does not create one for you.
Hey,

Yup - I understand that, but I'm always using my own constructor here. So why do I have to create a default one?
Some underlying operation might be using it without you knowing. Thats the only reason i can think of.
Topic archived. No new replies allowed.