【发布时间】: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< MyScalar,Point > 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()是一个非静态成员函数,你需要在实例上调用它。另外请注意,并非所有下划线都被禁止,在您的情况下,_Tp、MyScalar_和Scalar_将是被禁止的。 -
是的,从网页上看,它看起来像是一个静态变量......但是看看这个opencv.willowgarage.com/documentation/cpp/…你说没有遵循标准
标签: c++ templates opencv stl map