【问题标题】:c++ custom object in map throwing error地图中的c ++自定义对象抛出错误
【发布时间】:2012-11-15 23:33:44
【问题描述】:

我正在尝试在 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&lt; MyScalar,Point &gt; edgeColorMap;

上面的错误表明该行:

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

if 语句失败,我不知道为什么??

【问题讨论】:

  • 您为什么认为std::map::end 会起作用?另外,请阅读What are the rules about using an underscore in a C++ identifier?
  • 你说的下划线很有意思,我只是在关注OpenCV中的c++ api在做什么!
  • 嗯,唯一可以返回的东西是使用() 的函数,因为end() 是一个非静态成员函数,你需要在实例上调用它。另外请注意,并非所有下划线都被禁止,在您的情况下,_TpMyScalar_Scalar_ 将是被禁止的。
  • 是的,从网页上看,它看起来像是一个静态变量......但是看看这个opencv.willowgarage.com/documentation/cpp/…你说没有遵循标准
  • 您发布的链接似乎已失效。我从未使用过 OpenCV,但我发现了一些关于名称冲突的错误报告,因为它们不遵循标准 see herehere。如果您不遵守标准,就会发生这种情况。

标签: c++ templates opencv stl map


【解决方案1】:

您需要使用来自实际 map 实例的迭代器:

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

std::map::end() 只是一个普通函数,它返回一个迭代器或 const_iterator 到容器最后一个元素之后的元素。

【讨论】:

    猜你喜欢
    • 2019-04-19
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    相关资源
    最近更新 更多