2

im working with c++ and NS3 for my thesis. so i'd like to sort a vector of struct with the stl function sort so i'll post my header file where my vector is situated and i'll explain what i'd like to do.

#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;

};

the struct is this

struct CqiFeedback
  {
    /** the sub channel */
    int m_subChannelId;
    /** the cqi feedback */
    int m_cqi;
  };

i'd like to sort in descending order the vector m_cqiFeedbacks by the m_cqi parameter, the struct contained inside the header file miscellaneous.h. so i tried to overload the operator > in the previous way but i got this error:

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

i don't understand what is wrong!! may you help me please, i tried to read previous discussion, but i didn't realize very well what the problem is ....

4

1 回答 1

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;
}

应该

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

将运算符声明为friend类内部可以让您实现为自由运算符 - 在这种情况下采用 2 个显式参数。所以你已经声明了自由运算符,好吧,但是你声明了成员运算符,因为它inline有 2 个参数,加上隐含的this,所以总共有 3 个参数,这是错误的。

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

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

而且,当然,您可以更换

  if( m_cqi>b.mcqi ) return true;
    return false;

用一个简单的

  return m_cqi > b.mcqi
于 2012-11-05T16:44:05.600 回答