2012-11-05 38 views
2

即時工作與c + +和NS3。 所以我想排序結構的向量與stl函數排序,所以我會發布我的頭文件,我的向量所在,我會解釋我想要做什麼。如何爲我的論文命令一個向量結構

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

}; 

的結構是這

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

我想以降序由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 

我不明白什麼是錯的! 可能你幫我請,我試圖讀取前面的討論,但我沒有意識到很好的問題是什麼....

+7

「服務員,更換

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

結構向量請!「 –

+1

與問題無關,但您可以在比較函數中「返回a.m_cqi> b.mcqi;'。 – juanchopanza

+0

@KerrekSB:哈哈我的英雄 –

回答

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

,當然,你可以用一個簡單的

return m_cqi > b.mcqi 
+0

我不確定這是個好主意。如果'CqiFeedback'有一個普遍同意的順序(比如'int'),那麼任何查看'lhs_cqi =')。否則,只需定義一個排序功能對象,並將其傳遞給'std :: sort'。操作符不需要濫用僅僅使用'std :: sort'。 –

+0

謝謝你的答案,我試圖用stl的更大功能來解決它​​。 –