Consider the following class
#include <set>
#include <vector>
using namespace std;
class foo {
public:
struct spatial {
bool block;
char status; // H, M, Z
};
typedef pair< int, vector<spatial> > way; // tag + spatial vector
typedef set< way > one_set;
void bar()
{
way theWay;
theWay.first = 10;
one_set theSet;
one_set::iterator sit = theSet.end();
if (theSet.size() == 16) {
sit = theSet.begin();
}
theSet.insert(sit, theWay);
}
};
For the insert function, I receive these errors and I don't know what does that mean
error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const foo::spatial' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility
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 foo::spatial' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144
error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const foo::spatial' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144
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 foo::spatial' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144
error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const foo::spatial' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144
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 foo::spatial' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144
error C2676: binary '<' : 'const foo::spatial' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144
Stuck at this point. Appreciate any help.
Your way
type must have an operator<
defined to be used in an std::set
.
std::pair
has operator<
if both types has operator<
. int
has operator<
, that is ok, but vector<spatial>
has operator<
only if its element type has operator<
. Your spatial
class has none and therefore your way
typedef has no operator<
as well.
Create an operator<
for your spatial
class and you're ok.
If you think that spatial
classes should not be comparable but insist on having way
in a set, you can also create your own comparator (a functor/lambda with two const way&
parameters that return true
if the first is less than the second) and pass that as the second template parameter of your set.
Anyway, because std::set
stores its elements sorted, to use it you have to define the ordering of your elements at some point.
Depending on your use-case you can also use unordered_set
. There you need a operator==
and a hash-function:
#include <unordered_set>
#include <vector>
using namespace std;
class foo {
public:
struct spatial {
bool block;
char status; // H, M, Z
bool operator== (spatial const & that) const {
return block==that.block && status==that.status;
}
size_t hash() const {
return status & static_cast<int>(block) << 9;
}
};
typedef pair< int, vector<spatial> > way; // tag + spatial vector
struct way_hash {
size_t operator()(const way &w) const{
return w.first;
}
};
typedef unordered_set< way, foo::way_hash > one_set;
void bar()
{
way theWay;
theWay.first = 10;
one_set theSet;
one_set::iterator sit = theSet.end();
if (theSet.size() == 16) {
sit = theSet.begin();
}
theSet.insert(sit, theWay);
}
};
int main() {
foo f;
f.bar();
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.