-1

我试图在我的类 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++)
{

}
}
4

2 回答 2

0

parsePolygon 的定义应该是:

Polygon  Polygon::parsePolygon(std::string s) {
    ....
}
于 2013-07-07T07:47:33.713 回答
0

有很多问题。一个明显的问题是move()成员函数的定义。它从来没有真正定义过:你有一个同名的非成员函数。所以,你必须在Polygon范围内定义它。接下来,*this.size()不做你认为它做的事情:它被解析为*(this.size()). 无论如何您都不需要this,因此很容易修复:

void Polygon::move(int dx, int dy)
{
  for(int i = 0; i < getPolygon().size(); ++i)
  {

  }
}

您对 的定义有类似的问题parsePolygon,应该是

Polygon Polygon::parsePolygon(std::string s)
{
  ......
}
于 2013-07-07T08:01:14.230 回答