Home>

### c++ - i don't understand the error about using a member function

Question content&consideration

Please tell me the details of the error.
First, on line 58`Point`in class`void qij_rotate(double theta)`It is said that there is no such thing even though I am making.
The 76th line is the same as the 58th line.
Line 88 is`ri`Is defined just above but not so.
Both`Rectangle bounding_rectangle(std::vector&points)`In the function.

problem

error

``````kadai31.cpp: In function'Rectangle bounding_rectangle(std::vector<Point>&)':
kadai31.cpp:58:20: error:'class std::vector<Point>' has no member named'qij_rotate'
58 | points.qij_rotate(-qij);//pij ^~~~~~~~~~
kadai31.cpp:76:20: error:'class std::vector<Point>' has no member named'qij_rotate'
76 | points.qij_rotate(qij);//rijij ^~~~~~~~~~
kadai31.cpp:88:14: error:'ri' was not declared in this scope;did you mean'r'?
88 | return r[ri];
| ^~
|``````
code
``````// minimum bounding rectangle
#include<iostream>
#include<cmath>
#include<vector>
//Class that represents x and y coordinates
struct Point
{
double x;
double y;
// A member function that rotates counterclockwise about the origin by θ
void qij_rotate(double theta){
double dx=x;// Keep the x coordinate unchanged
x=dx*cos(theta)-y*sin(theta);
y=dx*sin(theta)+y*cos(theta);// Rotate the point (x,y) by θ around the origin, resulting in (xcosθ-ysinθ,xsinθ+ycosθ)
}
};
// A class that represents a rectangle
struct Rectangle
{
Point lower_left_corner;// represents the lower left corner of the rectangle
double width;// represents the width of the rectangle
double height;// represents the height of the rectangle
double angle;// represents the tilt angle of the rectangle
// A member function that rotates the rectangle counterclockwise about the origin by θ
void rotate(double theta)
{
Point d;
d.x=lower_left_corner.x;// Keep x coordinate unchanged
lower_left_corner.x=d.x*cos(theta)-lower_left_corner.y*sin(theta);
lower_left_corner.y=d.x*sin(theta)+lower_left_corner.y*cos(theta);
angle+=theta;
}
};
// A function that calculates the bounding rectangle of the point set
Rectangle bounding_rectangle(std::vector<Point>&points)
{
int i,j;
int n=points.size();
Point min,max;
min.x=points[0].x;
max.x=points[0].x;
min.y=points[0].y;
max.y=points[0].y;
std::vector<double>area;// array of areas
std::vector<Rectangle>r;// array of rectangles
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
double qij=std::atan2(points[j].y-points[i].y,points[j].x-points[i].x);// positive direction of x axis and line segment pipj Calculate the angle you make
points.qij_rotate(-qij);// rotate all points of p by -qij around the origin
// Find the minimum bounding rectangle parallel to the axis
if(points[i].x<min.x){
min.x=points[i].x;
}
if(points[i].x<max.x){
max.x=points[i].x;
}
if(points[i].y<min.y){
min.y=points[i].y;
}
if(points[i].y>max.y){
max.y=points[i].y;
}
Rectangle rij;
rij.height=max.y-min.y;// height
rij.width=max.x-min.x;// width
r.push_back(rij);
points.qij_rotate(qij);// rotate rij around the origin qij
double areas=rij.height*rij.width;// area of ​​rij
area.push_back(areas);
}
}
double min_area=area[0];
for(i=0;i<n;i++){
if(min_area>area[i]){
min_area=area[i];
int ri=i;
}
}
return r[ri];
}
int main()
{
int n;// number
int i;
std::cout<<"Please enter the number of points (n>=3)\n";
std::cin>>n;
std::vector<Point>pts;
pts.resize(n);
std::cout<<"Please enter the coordinates of the point\n";
for(Point&p:pts){
std::cin>>p.x>>p.y;
}
Rectangle rij=bounding_rectangle(pts);
std::cout<<"Minimum bounding rectangle width is<<<<rij.width<<" Height is<<<<rij.height;
return 0;

}``````

points.qij_rotate(-qij);

Since points is not a Point type.

`qij_rotate`There is an answer for, so only the other one.

Line 88 says ri isn't there, even though it's defined just above.

Variables cannot be used outside the block in which they are declared.

``````  for(i=0;i<n;i++){
if(min_area>area[i]){
min_area=area[i];
int ri=i;
} //<-ri does not exist after this.
}
return r[ri];``````