为什么我的类成员不存在?

Why does my class member not exist?

本文关键字:不存在 成员 我的 为什么      更新时间:2023-10-16

我写了一个地图类:

typedef int (& func(const std::string &));
template <class t_child>
class map final
{
std::vector<t_child> m_table;
const func m_hasher;
public:
explicit map(const func hasher) : m_hasher(hasher) {}
map(const map &copy) = delete;
~map();
map &operator=(const map&) = delete;
//***
};

但是我收到错误:

map.hpp:15: error: class ‘map<t_child>’ does not have any field named ‘m_hasher’
explicit map(const func hasher) : m_hasher(hasher) {}
^~~~~~~~

怎么了?为什么它不存在?

我知道这不是一个很好的答案,但如果这是你想要的,我可以告诉你一种快速让它工作的方法。

我通常使用<functional>来做这样的事情,因为它通常比函数指针语法更容易记住。我有以下编译。

#include <string>
#include <functional>
using func = std::function<int(std::string&)>;
template <class t_child>
class map final
{
std::vector<t_child> m_table;
const func m_hasher;
public:
explicit map(const func hasher) : m_hasher(hasher) {}
map(const map &copy) = delete;
~map();
map &operator=(const map&) = delete;
//***                                                                                                                                                                                          
};
int main() {
return 0;
}

这不一定与您的问题有关,但您也可以使用标准unordered_map类并在需要时继承它。