我正在用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()
在…上
子级(_C)
,我从中得到以下错误
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
}
}
我一辈子都搞不清楚编译器为什么不使用
数据树比较
比较
数据树
针对
std::字符串
.
我的问题是,为什么不这样做?如您所见,我已尝试向比较器添加运算符重载,其中
std::字符串
同时是左手操作员和右手操作员。
注:
我知道这里面缺少了很多基本特征
数据树
类型这项工作仍在进行中。