c ++ set<>类对象。使用自己的比较器为函数,xtree等提供JUNK错误

时间:2012-03-20 09:02:39

标签: c++ operator-overloading set

c ++中的代码如下:

#include<iostream>
#include<string>
#include<set>
using namespace std;

class data{
    int i;
    float f;
    char c;
public:
    bool operator()( data const& a1, data const& a2 ) const{
        return( a1.i < a2.i ||
            (!(a1.i > a2.i) && (a1.f < a2.f)) ||
            (!(a1.i > a2.i) && !(a1.f > a2.f) && (a1.c < a2.c)));
    };
    data();
    data(int i,float f,char c);
    int geti();
};

int data::geti(){
    return i;
};
data::data(int i,float f,char c){
    this->i=i;
    this->f=f;
    this->c=c;
};

int main(){
    set<data> s;
    set<data>::iterator it;
    s.insert(data(1,1.3,'a'));
    s.insert(data(2,2.3,'b'));
    s.insert(data(3,3.3,'c'));
    if((it=s.find(data(1,1.3,'a')))!=s.end())
        cout<<(*it).geti();
    cin.get();
    return 0;
}

此问题是在C++ set Question

中建议的修改之后

在编译时,它会从.. \ vc \ include \ functional和.. \ vc \ include \ xtree

中给出JUNK错误

请帮我解决错误。 感谢。

错误:

1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const data'
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(1372) : see declaration of 'std::operator <'
1>        c:\program files\microsoft visual studio 8\vc\include\functional(142) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1>        with
1>        [
1>            _Ty=data
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\set(60) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=data
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(26) : see reference to class template instantiation 'std::_Tset_traits<_Kty,_Pr,_Alloc,_Mfl>' being compiled
1>        with
1>        [
1>            _Kty=data,
1>            _Pr=std::less<data>,
1>            _Alloc=std::allocator<data>,
1>            _Mfl=false
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(68) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=std::_Tset_traits<data,std::less<data>,std::allocator<data>,false>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(94) : see reference to class template instantiation 'std::_Tree_ptr<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=std::_Tset_traits<data,std::less<data>,std::allocator<data>,false>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(112) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=std::_Tset_traits<data,std::less<data>,std::allocator<data>,false>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\set(69) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=std::_Tset_traits<data,std::less<data>,std::allocator<data>,false>
1>        ]
1>        c:\users\nitin_thokare\documents\visual studio 2005\projects\cpp\stl\ex_sets.cpp(31) : see reference to class template instantiation 'std::set<_Kty>' being compiled
1>        with
1>        [
1>            _Kty=data
1>        ]
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const data'
1>        c:\program files\microsoft visual studio 8\vc\include\string(151) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const data'
1>        c:\program files\microsoft visual studio 8\vc\include\string(141) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const data'
1>        c:\program files\microsoft visual studio 8\vc\include\string(131) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const data'
1>        c:\program files\microsoft visual studio 8\vc\include\xutility(1880) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const data'
1>        c:\program files\microsoft visual studio 8\vc\include\utility(76) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2676: binary '<' : 'const data' does not define this operator or a conversion to a type acceptable to the predefined operator

4 个答案:

答案 0 :(得分:1)

运算符&lt; 未命中,它可以方便地编写为成员函数,如下所示,或者作为类外的友元操作符:

int geti() const;

bool operator<( data const& a2 ) const {
    data const& a1 = *this;
    return( a1.i < a2.i ||
        (!(a1.i > a2.i) && (a1.f < a2.f)) ||
        (!(a1.i > a2.i) && !(a1.f > a2.f) && (a1.c < a2.c)));
};

你也忘记了const限定符:

int geti() const;

答案 1 :(得分:1)

使用提供operator<作为已提及的另一个,或使用data作为std::set的第二个模板参数。

请参阅std::set声明:

template<
    class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>
> class set;

因此,您可以使用std::set<data,data>作为您的设置类型。这样,您的operator()将用于比较data个对象。

答案 2 :(得分:1)

使用Boost,因为我厌倦了样板。

class data {
  ...
public:
  bool operator<(data const& other) const {
    return boost::tie(this->i, this->f, this->c)
         < boost::tie(other.i, other.f, other.c);
  }


};

比你的版本更多,更可读。

答案 3 :(得分:0)

data类型没有<所需的比较运算符std::set。你可能想要

bool operator < ( data const& a2 ) const {
    return( i < a2.i ||
        (!(i > a2.i) && (f < a2.f)) ||
        (!(i > a2.i) && !(f > a2.f) && (c < a2.c)));
}

而不是data::operator()