为什么我的类成员不存在?
Why does my class member not exist?
我写了一个地图类:
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 ©) = 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 ©) = delete;
~map();
map &operator=(const map&) = delete;
//***
};
int main() {
return 0;
}
这不一定与您的问题有关,但您也可以使用标准unordered_map类并在需要时继承它。
相关文章:
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 我们可以访问一个不存在的联盟的成员吗
- 为什么我的类成员不存在?
- 调用成员函数,尽管该对象不存在
- 就SFINAE而言,访问不存在的成员不被视为"error"吗?
- 在不存在的对象上调用静态成员函数
- 尽管使用不存在的成员,但程序编译
- 为什么C++中不存在对成员的引用?
- 初始化班级的不存在成员
- C++ vector 中不存在的字符串成员变量
- 类模板成员不存在错误
- 类成员突然变得不可访问/不存在
- 根据类成员的存在/不存在专门化C++模板
- 通过模板调用可能不存在的成员函数
- 访问可能不存在的const vector成员- try/catch或if (count != 0)
- WndProc和DispatchMessage的奇怪错误。成员函数不存在?
- Qt动画 - 成员不存在
- 对不存在的对象调用成员函数工作正常,c++
- 区分不明确的成员请求错误和 SFINAE 上下文中成员不存在的错误
- 如何在不知道成员类型的情况下与SFINAE核实成员是否存在?