简体   繁体   English

C ++自定义对象在地图中抛出错误

[英]c++ custom object in map throwing error

I'm trying to get a STL map in C++ working with custom scalar objects so that I can use the scalar class in a map for OpenCV. 我正在尝试使用自定义标量对象在C ++中获取STL映射,以便可以在OpenCV映射中使用标量类。 I'm getting the following error: 我收到以下错误:

error: 'template class std::map' used without template parameters

and this is the template im using: 这是模板即时通讯使用:

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;
};

I have std::map< MyScalar,Point > edgeColorMap; 我有std::map< MyScalar,Point > edgeColorMap; in my header file as well 在我的头文件中

the error above states that the line: 上面的错误指出该行:

auto tempit = edgeColorMap.find(s);
    if(tempit != std::map::end){//found a color that this pixel relates to

fails at the if statement and I can't figure out why?? 在if语句中失败,我不知道为什么?

You need to use an iterator from the actual map instance: 您需要使用实际map实例中的迭代器:

if(tempit != edgeColorMap.end()) {

std::map::end() is just a normal function which returns an iterator or const_iterator to the element following the last element of the container. std::map::end()只是一个普通函数,它将迭代器或const_iterator返回到容器最后一个元素之后的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM