Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;如何解决错误:无法转换‘;常量标准::uuCXX11::基本字符串<;char>’;至‘;常数键类型和’;{aka‘;const char&;x2019;}_C++ - Fatal编程技术网

C++ C++;如何解决错误:无法转换‘;常量标准::uuCXX11::基本字符串<;char>’;至‘;常数键类型和’;{aka‘;const char&;x2019;}

C++ C++;如何解决错误:无法转换‘;常量标准::uuCXX11::基本字符串<;char>’;至‘;常数键类型和’;{aka‘;const char&;x2019;},c++,C++,我得到Functionbool TTrie::hasChild(const数据类型&)const的错误: 无法将“const std::_cxx11::basic_string”转换为“const key_type&'{aka'const char&'}” 是因为.count()只接受char,而不是const数据类型& template< typename DataType> class TTrie { public: TTrie() { root = new TTri

我得到Function
bool TTrie::hasChild(const数据类型&)const
的错误:

无法将“const std::_cxx11::basic_string”转换为“const key_type&'{aka'const char&'}”

是因为
.count()
只接受
char
,而不是
const数据类型&

template< typename DataType>
class TTrie {
public:
  TTrie() {
    root = new TTrie();
  }


 /**                                                                                                         
   * Check whether a child linked by a specific value exists.                                                 
   * \param value a value                                                                                     
   * \return true if there is a link to a child labeled with the value,                                       
   *         false otherwise                                                                                  
   */
  bool TTrie<DataType>::hasChild(const DataType &value) const {
    std::map<char, TTrie<DataType>*> child = this->children;
    if(child.count(value)) {
      return true;
    }else{
      return false;
    }
  }

private:
  std::map<char, TTrie*> children;
  TTrie* root;
};





模板
类TTrie{
公众:
TTrie(){
root=新的TTrie();
}
/**                                                                                                         
*检查是否存在由特定值链接的子级。
*\param value一个值
*\如果存在指向标有该值的子级的链接,则返回true,
*否则就错了
*/
bool TTrie::hasChild(const数据类型和值)const{
std::map child=this->children;
if(子计数(值)){
返回true;
}否则{
返回false;
}
}
私人:
地图儿童;
TTrie*根;
};

这是一种非常迂回的编写方法
返回子项。count(value)>0
count
统计键的出现次数(因此对您来说是0或1),而不是值的出现次数。这里不容易说什么是正确的,因为不清楚您的类是什么。
数据类型的意义是什么?是否每个节点都持有
数据类型
char
和任意类型之间有什么联系?为什么您的地图存储指针?请显示
TTrie::children
的声明。顺便提一下,为什么要复制
this->children
,然后处理副本呢?很抱歉,我已经在代码中添加了更多细节。