0
我正在定義一個優先級隊列並將其用於自定義結構,但是我得到這個錯誤,我不知道如何解決它。錯誤C2678使用模板
這是我的錯誤:
error C2678: binary '<' : no operator found which takes a left-hand operand
of type 'const Location' (or there is no acceptable conversion)
我的結構位置
struct Location
{
int x, y, value;
Location(int a, int b);
bool operator == (const Location& other);
bool operator < (const Location& other);
};
Location:: Location(int a, int b) {
x = a;
y = b;
value = 0;
}
bool Location:: operator == (const Location& other) {
return (x == other.x && y == other.y);
}
bool Location:: operator < (const Location& other) {
return value > other.value;
}
這裏是我的優先級隊列
template<typename T>
struct my_priority_queue {
priority_queue<T, vector<T>, greater<T>> elements;
bool empty()
{
return elements.empty();
}
void push(T item)
{
elements.emplace(item);
}
T pop()
{
T best = elements.top();
elements.pop();
return best;
}
};
主要功能
int main() {
Location a(0, 0);
Location b(1, 2);
Location c(3, 0);
my_priority_queue<Location> my_pq;
my_pq.push(a);
}
'bool Location :: operator <(const Location&other)const' <=== note'const' addition。也屬於等價運算符(當然在class-def中)。 – WhozCraig
@WhozCraig非常感謝,它對我非常有幫助! –
@WhozCraig:http://meta.stackoverflow.com/q/320364/560648 –