Problem with class and member functions

I am not sure how to return a value for the brPoint and tlPoint of a triangle so I can compute the perimeter area and use a scalar to modify it. So far I have the program listed below but I am getting an error message the way I have it set up now (I can't change any variables or the order of the functions because of the instructions I am following). The errors occur in lines 129, 134, and 150. The error message is "error: request for member 'getLength' in '((const Triangle*)this)->Triangle::length', which is of non-class type 'const double' "

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <cmath>

using namespace std;

class Point
{
private:
	double px;
	double py;

public:
	void setX(const double x);
	void setY(const double y);
	double getX() const;
	double getY() const;
};

class Triangle
{
private:
	Point blPoint;
	double length, height;

public:
	// member functions
	void setBottomLeftX(const double x);
	void setBottomLeftY(const double y);
	void setLength(const double inLength);
	void setHeight(const double inHeight);

	Point getBottomLeft() const;
	Point getBottomRight() const;
	Point getTopLeft() const;
	double getLength() const;
	double getHeight() const;
	
	double perimeter() const;
	double hypotenuse() const;
	void scaleLength(const double sx);
	void scaleHeight(const double sy);
	void display() const;
};

// FUNCTION PROTOTYPES GO HERE:
void read_triangle(Triangle & tri);

int main()
{
	// Define local variables
	Triangle tri;
	double sx, sy;
	
	//Prompt the user for triangle information and fill Class Triangle object, tri,
	//with this information
	read_triangle(tri);
	
	// Display triangle information
	tri.display();
	
	// Prompt and read scale factors to change length and height
	cout << "Enter scale factor in x direction: ";
	cin >> sx;
	
	cout << "Enter scale factor in y direction: ";
	cin >> sy;
	
	// Apply scale factors
	tri.scaleLength(sx);
	tri.scaleHeight(sy);
	
	// Display triangle information
	tri.display();
	
	return 0;
}
		 
// FUNCTION DEFINITIONS GO HERE:
			 
// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:

void Point::setX(const double x) 
{
	px = x;
}

void Point::setY(const double y) 
{
	py = y;
}

double Point::getX() const 
{
	return (px);
}

double Point::getY() const 
{
	return (py);
}

void Triangle::setBottomLeftX(const double x) 
{
  blPoint.setX(x);
}

void Triangle::setBottomLeftY(const double y) 
{
  blPoint.setY(y);
}

void Triangle::setLength(const double inLength) 
{
  length = inLength;
}

void Triangle::setHeight(const double inHeight) 
{
  height = inHeight;
}

Point Triangle::getBottomLeft() const
{
return (blPoint);
}

Point Triangle::getBottomRight() const
{
 return (blPoint.getX() + length.getLength());
}

Point Triangle::getTopLeft() const
{
  return (blPoint.getY() + height.getHeight());
}

double Triangle::getLength() const
{
  return (length);
}

double Triangle::getHeight() const
{
  return (height);
}

double Triangle::hypotenuse() const
{
	double hypotenuse(0.0);
	hypotenuse = sqrt(pow(height.getHeight,2), pow(length.getLength(),2));
	return hypotenuse;
}

At line 129, height is a double, but you're trying to call a method, getLength(), on it as if it were a class.

Also, blPoint.getX() is a double, and if there were such a thing as height.getLength() it would also be a double,, so the result of (blPoint.getY() + height.getHeight()) would be a double.

But you've defined your function to return a Point.

Similar problems at line 134.

At line 150, height and length are doubles, but you're trying to call methods on them as if they were classes.
Last edited on
How could I compute the bottom right point then using the blPoint? All I need to do is add length to the x coordinate of the point.
All I need to do is add length to the x coordinate of the point.

But you need to decide what you want the method to return. A single number representing the X-coordinate (for getBottomRight())? Or a complete Point object?

Then make sure the method actually returns the thing that you want it to.
The real problem is you triangle class is incomplete its is only valid for right triangles positions on the X and Y axis.
Topic archived. No new replies allowed.