如何命令结构的向量

Giu*_*nte 2 c++ overloading operator-keyword

我正在为我的论文使用c ++和NS3.所以我想用stl函数排序对struct的向量进行排序,所以我将把我的头文件发布到我的向量所在的位置,我将解释我想做什么.

#include "ns3/net-device.h"
#include "ns3/object.h"
#include "ns3/log.h"
#include <vector>
#include <stdint.h>
#include "miscellaneous.h"


namespace ns3 {

/**
 * \brief The UeRecord class is developed in order to store at the eNodeB
 * all information (such as feedback cqi, mac address etc...) of a UE registered
 * into that eNodeB. All UE records are managed by the UeManager class
 */
class UeRecord : public Object
{
public:
  UeRecord ();
  ~UeRecord ();

  /**
   * \brief CqiFeedbacks represents a list of CQI feedbacks
   * sent by the UE. The downlink packet scheduler of
   * the eNB uses these values to assign accordingly
   * radio resources.
   */


  /**
   * \brief a list of CQI feedbacks
   */
  typedef std::vector<struct CqiFeedback> CqiFeedbacks;


  /**
   * \brief Creates a ue record of the UE registered into the eNB
   * \param ue the pointer of the ue device
   * \param enb the pointer of the enb device
   */
  UeRecord (Ptr<NetDevice> ue, Ptr<NetDevice> enb);

  /**
   * \brief Set the UE of the record
   * \param ue the pointer of the ue device
   */
  void SetUe (Ptr<NetDevice> ue);

  /**
   * \brief Get the UE of the record
   * \returns the pointer of the UE
   */
  Ptr<NetDevice> GetUe (void);

  /**
   * \brief Set the eNB of the record
   * \param enb the pointer of the enb device
   */
  void SetEnb (Ptr<NetDevice> enb);

  /**
   * \brief Get the eNB of the record
   * \returns the pointer of the eNB
   */
  Ptr<NetDevice> GetEnb (void);


  /**
   * \brief Set CQI feedbacks of the registered UE
   * \param cqiFeedbacks a list of CQI feedback
   */
  void SetCqiFeedbacks (CqiFeedbacks cqiFeedbacks);

  /**
   * \brief Get CQI feedbacks of the registered UE
   * \returns a list of CQI feedback
   */
  CqiFeedbacks GetCqiFeedbacks (void);


public:
      friend bool operator > (const struct CqiFeedback &a, const struct CqiFeedback &b);

    inline bool operator > (const struct CqiFeedback &a, const struct CqiFeedback &b)
  {
      if(a.m_cqi>b.mcqi) return true;
        return false;
   }

private:
  Ptr<NetDevice> m_ue;
  Ptr<NetDevice> m_enb;
  CqiFeedbacks m_cqiFeedbacks;

};
Run Code Online (Sandbox Code Playgroud)

结构是这样的

struct CqiFeedback
  {
    /** the sub channel */
    int m_subChannelId;
    /** the cqi feedback */
    int m_cqi;
  };
Run Code Online (Sandbox Code Playgroud)

我想按照m_cqi参数的顺序按向量m_cqiFeedbacks排序,结构包含在头文件miscellaneous.h中.所以我试图以前面的方式重载运算符>但我得到了这个错误:

debug/ns3/ue-record.h:121: error: ‘bool ns3::UeRecord::operator>(const CqiFeedback&, const CqiFeedback&)’ must take exactly one argument
Run Code Online (Sandbox Code Playgroud)

我不明白有什么不对!请你帮帮我,我试着读一下之前的讨论,但我没有意识到问题是什么....

Luc*_*ore 5

这个:

friend bool operator > (const struct CqiFeedback &a, const struct CqiFeedback &b);

inline bool operator > (const struct CqiFeedback &a, const struct CqiFeedback &b)
{
  if(a.m_cqi>b.mcqi) return true;
    return false;
}
Run Code Online (Sandbox Code Playgroud)

应该

friend bool operator > (const struct CqiFeedback &a, const struct CqiFeedback &b)
{
  if(a.m_cqi>b.mcqi) return true;
    return false;
}
Run Code Online (Sandbox Code Playgroud)

将运算符声明为friend类内部允许您实现为自由运算符 - 在这种情况下采用2个显式参数.所以你已经声明了自由运算符,好吧,然后你inline用2个参数声明成员运算符,加上隐含的this3个总参数,这是错误的.

或者,如果您想要成员运营商,请执行以下操作:

inline bool operator > (const struct CqiFeedback &b) const
{
  if( m_cqi>b.mcqi ) return true;
    return false;
}
Run Code Online (Sandbox Code Playgroud)

当然,你可以替换

  if( m_cqi>b.mcqi ) return true;
    return false;
Run Code Online (Sandbox Code Playgroud)

用一个简单的

  return m_cqi > b.mcqi
Run Code Online (Sandbox Code Playgroud)