我花了大约两个小时来解决这个问题,之前我曾访问过这些stackoverflow问题:
两个都没有帮助我,所以我在这里指出我的问题:
1)我有一个Polygon将Point2Ds 存储在列表中的类.该课程有两个成员函数:
public:
std::pair<Point2D,Point2D> closestPts() const;
private:
Tripel const& findClosestPts (std::vector<Point2D> const& P,
std::vector<Point2D> const& X,
std::vector<Point2D> const& Y) const;
Run Code Online (Sandbox Code Playgroud)
2)该类还包含一个struct Triple函数的返回值findClosestPts.我需要那个,因为函数需要返回两个点和一个距离:
struct Tripel {
Point2D pt1;
Point2D pt2;
float dist;
};
Run Code Online (Sandbox Code Playgroud)
现在问题在于Polygon.cpp的实现.这是我上面提到的两个函数的(当前)代码:
std::pair<Point2D,Point2D> Polygon::closestPts() const {
...
int size = m_points.size();
std::vector<Point2D> P (size);
std::vector<Point2D> X (size);
std::vector<Point2D> Y (size);
...
// some manipulation of the vectors, filling them with Point2D
// at this point, I have three non-const std::vector<Point2D>
// try to call the other function
Tripel closPts = findClosestPts(P, X, Y);
...
}
Tripel const& findClosestPts (std::vector<Point2D> const& P, std::vector<Point2D> const& X, std::vector<Point2D> const& Y) const {
...
}
Run Code Online (Sandbox Code Playgroud)
编译器错误是:
error: non-member function 'const Tripel& findClosestPts(...)' cannot have cv-qualifier
Run Code Online (Sandbox Code Playgroud)
所以我想我不允许这个功能const,因为它返回一个struct.真的吗?
无论如何,我将函数签名更改为:
Tripel const& findClosestPts (std::vector<Point2D> const& P,
std::vector<Point2D> const& X,
std::vector<Point2D> const& Y);
Run Code Online (Sandbox Code Playgroud)
所以,功能已经const不复存在了.这会导致以下编译错误:
error: passing 'const Polygon' as 'this' argument of 'const Tripel& Polygon::findClosestPts(...)' discards qualifiers [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
我不知道,现在该做什么.我几乎尝试了所有的东西,删除所有的const语句,更改它们,findClosestPts公开,再次使它成为const,使三个std :: vectors const在传递给另一个函数之前......但是所有内容都导致(不同)编译错误.
所以我的问题是,我如何编写这两个函数,以实现以下目标:我想拥有closestPoints()一个公共成员函数的函数,并返回两个最接近点的对.为此它需要一个辅助的私有成员函数findClosestPts(vector1, vector2, vector3)返回上面提到的struct Triple?
我很乐意帮忙,因为我有一段时间被困在这里:/
你可以做到const,你只是忘了在实现中限定名称.
//class name
||
\/
Tripel const& Polygon::findClosestPts (std::vector<Point2D> const& P,
std::vector<Point2D> const& X, std::vector<Point2D> const& Y) const
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
548 次 |
| 最近记录: |