Unable to get center() function to work properly

My center function in class Point below doesn't work as I want it to. In my main, when I reduced 'Point.center().x()' to 'Point.x()', it compiles fine and outputs the correct value. But when center() is involved, comes that issue. I'm not allowed to modify my main. Was just testing on my own. Can anyone tell me where I've gone wrong in my code? Thanks!

Error msg:
request for member ‘x’ in ‘point.Point::center()’, which is of non-class type ‘float’
if (is_near(point.center().x(), x) &&
^
....
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

class Object
{
	public:
	struct PointType
{
	float x;
	float y;
	PointType( float x1,  float y1) :x(x1),y(y1){}
};
	private:
	float d;
	PointType * pt;
	public:
	Object(float n) : d(n){}
	 
     float depth() const
	{
		return d;
	}
};
class Point :public Object 
{
 
private:
    PointType mpoint;

public:

    Point(const PointType& pt, float& y1) : mpoint(pt), Object(y1) {}

 	 float center() //Not working properly
	{
	     x();   y();
	}
	float x()
	{
		return mpoint.x;
	}
	float y()
	{
		return mpoint.y;
	}
};

const float EPSILON = 1e-5f;

bool is_near(float x, float y)
{
	return std::abs(x - y) < EPSILON;
}

float frand()
{
	return 10.0f * float(rand()) / float(RAND_MAX);
}

int main()
{
	srand(unsigned(time(0)));
	int count = 0;
	int max_count = 0;

	float x = frand();
	float y = frand();
	float depth = frand();

	Point point(Point::PointType(x, y), depth);
	if (is_near(point.depth(), depth))
	{
		++count;
	}
	else
	{
		std::cout << "  - Point::depth test failed" << std::endl;
	}
	++max_count;
 
	if (is_near(point.center().x(), x) &&
		is_near(point.center().y(), y))
		
	{
		++count;
	}
	else
	{
		std::cout << "  - Point::center test failed" << std::endl;
	}
	std::cout << point.center().x() << " "<<x<<std::endl;
		std::cout << point.center().y() << " "<<y<<std::endl;

	++max_count;
}
center method should not be a float. it should be a point class type. it returns a float, which does not have any fields (you cant say float z; z.x; no such thing.... this is what you are doing). when it returns the correct object, object.x() will work....
Last edited on
Hi again jonnin, thanks for the help. I tried having Point earlier on, it did compile but the output wasn't correct. Maybe I'll try again. so just to clarify on what u just said, the point center() function should return a float? Sorry if I didn't understand properly.

no. that is the opposite of what I said :)

it SHOULD NOT return a float, it MUST return a point.
do you see why?
down in the main
point.center().x … ( pointer.center() ) <--- that is a thing by itself. it is whatever center returns. if its a float, you are done. floats do not have a.x that you can use. if its a point, it makes sense to call .x on it... it has one!

what it *really* does is create a hidden temporary point object (or it will once you fix it!) so you can use the hidden object's x member, copy it out, and the temporary will be gone.
Last edited on
Alright I solved it! Thanks again jonnin for your invaluable explanation!:)
Topic archived. No new replies allowed.