映射引发错误中的 C++ 自定义对象
c++ custom object in map throwing error
我正在尝试在C++使用自定义标量对象来获取 STL 映射,以便我可以在 OpenCV 的映射中使用标量类。我收到以下错误:
error: ‘template class std::map’ used without template parameters
这是我使用的模板:
template<typename _Tp> class MyScalar_ : public Scalar_<_Tp>{
public:
MyScalar_();
MyScalar_(Scalar_<_Tp>& s){
_s = s;
};
_Tp& operator[](const int idx){
return _s[idx];
}
//std::less<_Tp>::operator()(const _Tp&, const _Tp&) const
//this wont work if scalars are using doubles
bool operator < (const MyScalar_<_Tp>& obj) const {
double lhs,rhs;
lhs = _s[0] + _s[1] + _s[2] + _s[3];
rhs = _s[0] + _s[1] + _s[2] + _s[3];
return lhs > rhs;
}
bool operator == (const MyScalar_<_Tp>& obj) const{
bool valid = true;
for(int i = 0;i<_s.size();i++){
if(_s[i] != obj[i])
return false;
}
return valid;
}
private:
Scalar_<_Tp> _s;
};
我的头文件中也有std::map< MyScalar,Point > edgeColorMap;
上面的错误指出该行:
auto tempit = edgeColorMap.find(s);
if(tempit != std::map::end){//found a color that this pixel relates to
在 if 语句中失败,我不知道为什么??
您需要
使用实际map
实例中的迭代器:
if(tempit != edgeColorMap.end()) {
std::map::end()
只是一个普通函数,它将迭代器或const_iterator返回到容器最后一个元素之后的元素。
相关文章:
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 如何正确实现和访问运算符的各种自定义枚举器
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 自定义先决条件对移动分配运算符有效吗
- 使用VS Code和CMake Tools运行自定义命令
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- std::ranges::elements_view,用于自定义类似元组的数据
- 跟随整数索引列表的自定义类迭代器
- 参数化自定义CMake工具链
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 如何在自定义类中启用'auto loops'?
- 使用QJsEngine在Qt中注册自定义类型
- Qt自定义QPush按钮未显示在布局上
- 自定义对象的dlib序列化在gcc中失败
- 自定义创建QFuture
- 如何在QT中的自定义视频小工具t上绘制矩形