Compilation error

I am getting a compilation error which I can't resolve on my own. Don't really know what to change really. Been stuck on this. Need your expertise on this please. Note:main() can't be modified. Thanks!

error: expression cannot be used as a function
is_near(polygon[index].x(), xs[index]) &&

object_polygon_test.cpp:59:28: error: expression cannot be used as a function
is_near(polygon[index].y(), ys[index]))


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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class Object
{
public:

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

    Point(const PointType& pt, float& y1) : mpoint(pt), Object(y1) {}
	
  	 Point center() 
	{
	     return *this;
	}
	 float x()
	{
		return mpoint.x;
	}
	 float y()
	{
		return mpoint.y;
	}
};
class Polygon :  public Point
{
private:
	Object::PointType *mpt;
	int msize;
	
public:
	 const PointType& operator[](int index)  
	 {
		 return mpt[index];
	 }
	Polygon(PointType* points, int npoints, float depth)
	:  msize(npoints), mpt{new Object::PointType[npoints]}, Point(*mpt,depth){
		for(int i = 0; i < msize; ++i)
		{
		 mpt[i]  = points[i];
		}
	}
	 float x()
	{
		return mpt-> x;
	}
	 float y()
	{
		return mpt-> y;
	}
};
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

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;

	int max_index = 3 + rand() % 8;
	float* xs = new float[max_index];
	float* ys = new float[max_index];
	float depth = frand();
	float x = 0;
	float y = 0;
	Polygon::PointType* vertices = new Polygon::PointType[max_index];
	for (int index = 0; index < max_index; ++index)
	{
		xs[index] = frand();
		ys[index] = frand();
		vertices[index] = Polygon::PointType(xs[index], ys[index]);
		x += xs[index];
		y += ys[index];
		y += ys[index];
	}
	x /= static_cast<float>(max_index);
	y /= static_cast<float>(max_index);
	Polygon polygon(vertices, max_index, depth);
	delete[] vertices;
        int index = 0;
	while ((index < max_index) &&
		is_near(polygon[index].x(), xs[index]) &&
		is_near(polygon[index].y(), ys[index]))
	{
		++index;
	}
	if (index == max_index)
	{
		++count;
	}
	else
	{
		std::cout << "  - Polygon::operator[] test failed" << std::endl;
	}
	++max_count;
 
Last edited on
Whenever you see something like x(), it likely means you are trying to call a function named “x”.

So when you say polygon[index].x(), you are trying to call the member function “x” in a Polygon.

But a Polygon does not have a function named “x”.

It does, however, have a variable named “x”.
So, update your code: polygon[index].x. Notice the lack of parentheses?

    is_near( polygon[index].x, xs[index] )

Hope this helps.

[edit] I need to read better, apparently. Sorry.
Last edited on
Hi Duthomhas, I'm really sorry for not informing that I can't actually modify the main() file.
It is restricted by my school. Thanks for ur help. Is there any other way? I am aware that it is trying to call a function named x. But I did already create one didn't I? float x() and float y(). What can I do from here on?
Last edited on
Sorry, give me a sec.

--- Sorry again. I had to go away and do stuff for a bit. Here’s the promised answer. ---

Okay, you did make those functions, but you are returning a PointType object from your accessor:

1
2
3
4
	 const PointType& operator[](int index)  
	 {
		 return mpt[index];
	 }

And PointType has only fields x and y.

I think you are a little overloaded with the design. You need to reduce the number of types (classes/structs) you have. Unless your assignment specifically says otherwise, you also do not need to derive everything from a common Object.

Hope this helps.
Last edited on
when you do `polygon[index]' you get a PointType
`PointType' does not have a `x()' function.

by the way, you may want to review your class hierarchy, ¿a polygon is a point?
also
1
2
3
4
Polygon(PointType* points, int npoints, float depth):
   msize(npoints),
   mpt{new Object::PointType[npoints]},
   Point(*mpt,depth) //`mpt' is uninitialized here (check out: order of construction), you are dereferencing an invalid pointer 
Topic archived. No new replies allowed.