使用类型的成员作为std :: multiset中的键

时间:2014-08-03 15:11:27

标签: c++ c++11 functor multiset

我在C ++中创建自己的树状容器。以下是我对其结构的概述:

//==========================================================
// Concept
//==========================================================
//
// RootNode -> Attributes
//          -> Nodes -> Attributes
//                   -> Nodes -> Attributes
//                            -> Nodes
//                             
// etc...
//==========================================================

我现在已将类型命名为DataTree。标题如下所示:

#pragma once

#include <map>
#include <set>
#include <string>

namespace LB{
    class DataTree;
    struct DataTreeComparitor{
        bool operator () (DataTree const & lhs, DataTree const & rhs) const;
        bool operator () (std::string const & lhs, DataTree const & rhs) const;
        bool operator () (DataTree const & lhs, std::string  const & rhs) const;
    };
    class DataTree{
    public:
        template <typename Type>
        Type getAttribute(std::string const & name) const{
            static_cast<Type>(*m_attributes.find(name));
        }

        DataTree getChild(std::string const & name) const;

        std::string getName(void) const;

    private:
        std::string m_name;

        std::multimap<std::string, std::string> m_attributes;
        std::multiset<DataTree, DataTreeComparitor> m_children;
    };
}

我想使用m_children在容器std::string中找到元素,DataTreeComparitor将与getName()的结果进行比较。

但是,在find()上尝试拨打m_children时,我从mingw32收到以下错误:

    C:/Users/Joshua/Documents/BitBucket/LBC++/LBC++/DataTree.cpp:17:31: error: no matching function for call to 'std::multiset<LB::DataTree, LB::DataTreeComparitor>::find(const string&) const'

这是使用MSVC

    2   IntelliSense: no instance of overloaded function "std::multiset<_Kty, _Pr, _Alloc>::find [with _Kty=LB::DataTree, _Pr=LB::DataTreeComparitor, _Alloc=std::allocator<LB::DataTree>]" matches the argument list
            argument types are: (const std::string)
            object type is: const std::multiset<LB::DataTree, LB::DataTreeComparitor, std::allocator<LB::DataTree>> c:\Users\Joshua\Documents\Visual Studio 2013\Projects\Project1\Project1\Source.cpp  46  21  Project1

这是实现,问题行通过注释表示:

#include "DataTree.h"

namespace LB{
    bool DataTreeComparitor::operator () (DataTree const & lhs, DataTree const & rhs) const{
        return lhs.getName() < rhs.getName();
    }

    bool DataTreeComparitor::operator () (std::string const & lhs, DataTree const & rhs) const{
        return lhs < rhs.getName();
    }

    bool DataTreeComparitor::operator () (DataTree const & lhs, std::string  const & rhs) const{
        return lhs.getName() < rhs;
    }

    DataTree DataTree::getChild(std::string const & name) const{
        return *m_children.find(name); // problem line
    }
}

我不能为我的生活解决为什么编译器没有使用DataTreeComparitor来比较DataTreestd::string

我的问题是,为什么不这样做?正如您所看到的,我尝试向比较器添加运算符重载,其中std::string既是左手操作员又是右手操作员。

注意:

我知道此DataTree类型中缺少许多基本功能;它仍在进行中。

1 个答案:

答案 0 :(得分:2)

问题是您使用multiset<DataTree, DataTreeComparitor>::find参数调用std::string。这不起作用,因为它需要DataTree,并且不会从std::string转换为DataSet

您可以通过添加隐式转换构造函数来解决此问题:

class DataTree{
public:
  DataTree(const std::string& name) : m_name(name) {}
  ....
};