我正在尝试编译其中std :: map首先是类指针的类。当我创建类时,我会向前声明该类,但是如果我尝试取消对指针的引用,它会说operator

#ifndef _FoEcApplData_h_
#define _FoEcApplData_h_

#include <boost/serialization/string.hpp>

class RWDlistCollectables ;

#include "EcTypes.h"
#include "EcBcArch.h"

const RWClassID FoCEcApplData = 90;

// forward declare so we can declare FdTEcApplDataMap typedef
class FoEcApplData;

/*
Custom KeyEqual for FoEcApplData class on FdTEcApplDataMap.
Needed since we are using pointers.

Note:  Each FoEcApplData is unique so you can't be equal.

Returns - true less then, false equal and greater

lhs  - pointer to the item you are looking for
rhs - pointer to an item on the list
*/
struct FoEcApplDataPComp
{
        bool operator()(FoEcApplData *const& lhs, FoEcApplData *const& rhs) const
    {
        return (*lhs < *rhs);
    }
};
/*
NOTE:
You can NOT use the map find() call because the "operator<" is testing the array
length vs. what's inside the array.  Instead you must use FoEcApplData.find().
*/
typedef std::map<FoEcApplData *, std::string, FoEcApplDataPComp> FdTEcApplDataMap;


// FoEcApplData : public EcRwCollectable
//
//   This is the application data for a directive.  That is the binary for
// the directive alone without a packet header.

class FoEcApplData : public EcRwCollectable {

    EcMBcArchSplitDeclareClass(FoEcApplData,EcRwCollectable)

public:

    // FoEcApplData
    //
    // Default Constructor.

    FoEcApplData() ;

    // FoEcApplData
    //
    // Copy Constructor.

    FoEcApplData( const FoEcApplData& object ) ;

    // FoEcApplData
    //
    // Destructor.

    virtual ~FoEcApplData() ;

    // operator=
    //
    // Set this object equal to input object.

    FoEcApplData& operator=( const FoEcApplData& object) ;

    // operator==
    //
    // Test to see if this is equal to input object.

    bool operator==( const FoEcApplData& object) const ;

    // operator<
    //
    // Test to see if input object is less than input object.

    bool operator<( const FoEcApplData& object) const ;

    // operator>
    //
    // Test to see if input object is greater than input object.

    bool operator>( const FoEcApplData& object) const ;

    // operator+=
    //
    // Add the input applicationsd data to my own appending
    // to end of binary array.   Returns a reference to self.

    FoEcApplData& operator+=( const FoEcApplData& object ) ;

    /*
    Print out the class in a human readable format

    Returns - output stream
    os - output stream
    orig - Reference to class so we can print it out
    */
    friend std::ostream& operator<<(std::ostream& os, const FoEcApplData& orig);

    // find
    //
    // The std::map find call can't work because the operator< is not testing the array contented.
    // This will loop through the map key and compare each once for a match.
    //
    // returns - the iterator for the search
    //
    // applDataMap - the map list reference
    // object - The object you are trying to find in the map list

    FdTEcApplDataMap::const_iterator find(const FdTEcApplDataMap& applDataMap, const FoEcApplData * pObject);
    // Data
    //
    // Get the binary data.

    inline const EcTOctet* Data() const;

    // NumOfBytes
    //
    // Size of the binary in bytes.  0 indicates no binary data.

    inline EcTInt NumOfBytes() const;

    // Data
    //
    // Set the binary. I take possesion of the data.

    inline EcTVoid Data( EcTOctet* data , const EcTInt& dataSize ) ;

    // binaryStoreSize
    //
    // Computes the size of the class when stored using saveGuts.

    virtual RWspace binaryStoreSize() const ;

    // hash
    //
    // Returns a value suitable for a hash table.

    virtual EcTUInt hash() const ;

    // restoreGuts
    //
    // Recreates a class from file. (persistance)
    //
    // RWFileErr - thrown on file error.

    virtual EcTVoid restoreGuts( RWFile& file) ;

    // restoreGuts
    //
    // Recreates a class from stream. (persistance)

    virtual EcTVoid restoreGuts( RWvistream& inStream ) ;

    // saveGuts
    //
    // Saves a class to a file. (persistance)
    //
    // RWFileErr - thrown on file error.

    virtual EcTVoid saveGuts( RWFile& file) const ;

    // saveGuts
    //
    // Saves a class to a stream. (persistance)

    virtual EcTVoid saveGuts( RWvostream& outStream ) const ;

    // getDataMemberDifferences
    //
    // Compare the all of the data members that are streamed out.
    // Returns true if it found descrepancies. It also fills a collection
    // with strings containing all of the differences and possible
    // causes

    virtual EcTBoolean
    getDataMemberDifferences(
        const FoEcApplData* compareApplData,
        RWDlistCollectables& listOfDescrepancies) const;

private:

    // The actual data (binary).  This is a pointer to an array of
    // EcTOctet that is myNumOfBytes long.

    EcTOctet* myData;

    // The length of the binary in bytes.

    EcTInt myNumOfBytes;

};

// Include inline functions...
#ifndef OUTLINE
#include "FoEcApplData.iC"
#endif

BOOST_CLASS_EXPORT_KEY(FoEcApplData)

#endif

最佳答案

FoEcApplDataPComp::operator()的定义之后提供FoEcApplData的定义:

struct FoEcApplDataPComp {
    inline bool operator()(FoEcApplData const* lhs, FoEcApplData const* rhs) const;
};

class FoEcApplData : public EcRwCollectable { ... };

bool FoEcApplDataPComp::operator()(FoEcApplData const* lhs, FoEcApplData const* rhs) const {
    return *lhs < *rhs;
}


也将FoEcApplData *const&更改为FoEcApplData const*。部分原因是FoEcApplData::operator() const,另一部分原因是纯指针应被值接受。

关于c++ - typedef operator()未针对std::map排序进行编译,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54810859/

10-09 18:35