我试图在我的类 Polygon.cpp 中访问头类 Polygon.h 中的私有向量数组我尝试使用 getter,但它不允许我使用类本身的实例调用该函数。我该怎么办?
#include <string>
#include <vector>
#include "point.h"
class Polygon {
private:
std::vector<Point> pts;
public:
Polygon();
static Polygon parsePolygon(std::string s);
std::string toString() const;
void move(int dx, int dy);
double perimeter() const;
double area() const;
int getNumVertices() const;
bool operator == (const Polygon &p) const;
double isperimetricQuotient() const;
Point getIthVertex(int i) const {return pts[i];}
std::vector<Point> const &getPolygon() const {return pts;}
};
//Polygon.cpp
#include "Polygon.h"
#include <string>
Polygon::Polygon()
{
pts.resize(4);
Point p1(-1,1);
Point p2(1,1);
Point p3(1,-1);
Point p4(-1, -1);
pts[0] = p1;
pts[1] = p2;
pts[2] = p3;
pts[3] = p4;
}
static Polygon parsePolygon(std::string s)
{
int seperator;
int start = 1;
for(int i = 0; seperator != std::string::npos; i++)
{
seperator = s.find(")(");
std::string subPoint = s.substr(start, seperator);
getIthVertex(i) = Point.parsePoint(subPoint);
start = seperator + 1;
}
}
void move(int dx, int dy)
{
for(int i = 0; i < *this.getPolygon().size(); i++)
{
}
}