【问题标题】:passing values in a function that returns a struct - const errors在返回结构的函数中传递值 - const 错误
【发布时间】:2012-04-20 09:04:15
【问题描述】:

我在这个问题上花了大约两个小时,我之前访问过这些 stackoverflow 问题:

c++ passing a const object reference to a function

Passing const& as a function argument

两者都没有帮助我,所以我在这里说明我的问题:

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;

2) 该类还包含一个struct Triple,它是函数findClosestPts 的返回值。我需要那个,因为函数需要返回两个点和一个距离:

struct Tripel {
  Point2D pt1;
  Point2D pt2;
  float   dist;
};

问题现在出现在 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 {
   ...
}

编译错误是:

error: non-member function 'const Tripel& findClosestPts(...)' cannot have cv-qualifier

所以我想我不能创建这个函数const,因为它返回一个struct。这是真的吗?

无论如何,我将函数签名更改为:

Tripel const& findClosestPts (std::vector<Point2D> const& P,
                              std::vector<Point2D> const& X,
                              std::vector<Point2D> const& Y);

所以,函数不再是const。这会导致以下编译错误:

error: passing 'const Polygon' as 'this' argument of 'const Tripel& Polygon::findClosestPts(...)' discards qualifiers [-fpermissive]

我不知道,现在该怎么办。我几乎尝试了所有方法,删除所有 const 语句,更改它们,使 findClosestPts 公开,再次使其成为 const,在将三个 std::vectors 传递给另一个函数之前使它们成为 const ......但一切都导致(不同)编译错误。

所以我的问题是,我需要如何编写这两个函数来实现以下目标:我想要一个函数closestPoints(),它是一个公共成员函数,它返回两个最近点的对。为此,它需要一个辅助的私有成员函数findClosestPts(vector1, vector2, vector3),它返回上面提到的struct Triple?

我会很高兴得到帮助,因为我被困在这里已有一段时间了:/

【问题讨论】:

  • 您确定要返回 const 引用吗?这很容易导致悬空引用。 Triple 似乎无论如何都是一个值类型。
  • 困惑:是Triple === Tripel 吗?
  • @Robᵩ 我很确定他们的意思是一样的?英语与德语。 ;-)
  • 哦,是的……那是一个错字;)是的,你猜对了我的国籍:)

标签: c++ struct constants


【解决方案1】:

您可以设为const,只是忘记在实现中限定名称。

          //class name
                ||
                \/
Tripel const& Polygon::findClosestPts (std::vector<Point2D> const& P, 
           std::vector<Point2D> const& X, std::vector<Point2D> const& Y) const

【讨论】:

  • 该死的打字速度,我以为我很快
【解决方案2】:

提示在错误信息中:

error: non-member function ...

此时您可以停止阅读,因为您的编译器认为您的函数是一个非成员函数。该行上的任何进一步文本都基于您的编译器做出不正确的结论(基于您想要的)。解决方案是将Polygon:: 限定符添加到您的成员函数实现中:

Tripel const& Polygon::findClosestPts( ...

【讨论】:

    【解决方案3】:

    你需要用类名来限定函数名

    Tripel const& Polygon::findClosestPts (std::vector<Point2D> const& P, std::vector<Point2D> const& X, std::vector<Point2D> const& Y) const
    

    Luchian 打败了我,但错误告诉你 Non-member 不能有 cv 限定符,原因与静态函数不能被 cv 限定的原因相同

    【讨论】:

      【解决方案4】:

      请注意你最好回到这里:

      Tripel const& findClosestPts (std::vector<Point2D> const& P,  
                                      std::vector<Point2D> const& X,
                                      std::vector<Point2D> const& Y) const;
      

      Tripel 的值,而不是对它的引用。 正如有人已经指出的那样,返回引用是不安全的。

      这样的函数几乎总是必须返回一个值,而不是一个引用, 否则,您将需要保持返回引用的对象的生命周期以及接受相应引用的对象/代码的生命周期。即返回对象的生命周期必须保持在接收对象或代码的生命周期之内。

      【讨论】:

      • 还有一个问题:我现在应该退回它const 还是就这样?
      • 如果按值返回一个对象,这始终是一个临时对象,它是否为 CONST(或任何其他修饰符,如 volatile 等)都没有关系,因为该对象实际上不能被重用, 完全属于接受码,这里不需要限制什么。 ~ 90% 的 C++ 代码按值返回对象,不带修饰符。
      【解决方案5】:

      好吧...这不值得做所有的工作...我只是忘记了班级限定符Polygon::这真的很尴尬,我很抱歉:(

      谢谢大家的帮助,我想问题已经解决了!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 2018-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多