hi,
i am trying to use ofColor as a key in a multimap, but receive an “invalid operands to binary expression” error although i am not comparing values and have no need to overload an operand.
.h:
multimap<ofColor, string> colorMap;
multimap<ofColor, string>::iterator colorMapIt;
.cpp:
colorMap.insert(make_pair(ofColor(random(255)), "null"));
i tried creating a class to handle the ofColor and used that class in my multimap declaration, but the error was still the same.
any ideas?
thanks in advance.
01
With std::map, even if you’re not comparing values, you must ensure that the keys are not equivalente, and keys A and B are equivalent when neither A < B nor B < A is true, so you must override operator < (I think you can override == or another one, but you must override one of the comparison operators).
If you read the documentation in C++ reference for multimap, it says it stores elements following a specific order, so some kind of comparison is made. Try to override the < operator and see if this solves the problem
http://www.cplusplus.com/reference/map/multimap/
hi ainsoph,
i see what you mean about order, which is spoken about here (paragraph 2):
http://en.cppreference.com/w/cpp/container/multimap
since in a multimap, the keys can be equivalent, by overriding the < operator, am i trying to have it not do the comparison as defined in the template displayed here? shouldn’t i also have to override the > and = operators as well?
in this case:
two values are sent as parameters to a function and are compared. with my issue, i am adding one value, ofColor, to a map at a time. if the comparison being done between ofColor[0] and ofColor[1] and i am adding one value to the map at a time, how do you overload an operator with only one value?
i am assuming (if i get this) that i need a non-member overload that accepts the current and previous value of ofColor to be compared?
class ofClass: public ofBaseApp {
public:
.
.
.
class cColor {
public:
cColor();
~cColor();
ofColor c;
cColor(ofColor rColor) {
c = rColor;
}
};
static bool operator < (const currentColor& C1, const previousColor& C2) {
return C1 < C2;
}
};
01
so, this works:
class ofClass: public ofBaseApp {
public:
.
.
.
class cColor {
public:
ofColor c;
cColor(ofColor rColor) {
c = rColor;
}
friend bool operator< (const cColor &a, const cColor &b) {
};
};
inline bool operator< (const ofClass::cColor& C1, const ofClass::cColor& C2) {
return C1.c < C2.c;
}
multimap<cColor, string> colorMap;
multimap<cColor, string>::iterator colorMapIt;
however, it seems that sending ofColor as a parameter to a multimap/map does not:
colorMap.insert(make_pair(pinColor(ofColor(255)), "whatever"));
if the type in the class is changed to an int (and i assume any other type that can be ovrloaded), it works. but ofColor, is not one of those types.