我正在制作基本cpp程序的问题。我正在尝试为该程序编写基础知识。
#include <cstdlib>
#include <queue>
#include <iostream>
#include <set>
using std::cout;
using std::endl;
int main(int argc, char** argv) {
struct Point{
int x;
int y;
int id; // start or end of line? 1 = start, 2 = end
};
struct Line{
int id; // used when printing intersections
Point first;
Point second;
};
Point p1;
Point p2;
p1.x = 7;
p1.y = 5;
p1.id = 1;
p2.x = 11;
p2.y = 14;
p2.id = 2;
Line l1;
l1.first = p1;
l1.second = p2;
l1.id = 1;
cout << l1.first.x << endl;
std::priority_queue<Point> q; //priority queue ordered on x coordinates
std::set<Point> b; //binary tree ordered on y coordinates
q.push(p1);
q.push(p2);
while (!q.empty())
{
cout << "not empty" << endl;
if (q.top().id == 1)
{
cout << "start point" << endl;
}
q.pop();
}
return 0;
}
当我尝试运行时,我收到错误
In file` included from intersections.cpp:15:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:169:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/deque:158:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__split_buffer:7:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:628:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:606:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:343:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__functional_base:63:21: error: invalid operands to binary expression ('const Point' and 'const Point')
{return __x < __y;}
~~~ ^ ~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:4820:13: note: in instantiation of member function 'std::__1::less<Point>::operator()' requested here
if (__comp(*__ptr, *--__last))
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:4848:5: note: in instantiation of function template specialization 'std::__1::__sift_up<std::__1::less<Point> &, std::__1::__wrap_iter<Point *> >' requested here
__sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:649:12: note: in instantiation of function template specialization 'std::__1::push_heap<std::__1::__wrap_iter<Point *>, std::__1::less<Point> >' requested here
_VSTD::push_heap(c.begin(), c.end(), comp);
^
intersections.cpp:77:7: note: in instantiation of member function 'std::__1::priority_queue<Point, std::__1::vector<Point, std::__1::allocator<Point> >, std::__1::less<Point> >::push' requested here
q.push(p3);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/utility:430:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Point'
operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
1 error generated.
make[2]: *** [build/Debug/GNU-MacOSX/intersections.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
在我看到#include <queue>
的顶部,它显示包含的文件。
我不知道这是否导致了这个问题。
提前致谢。
答案 0 :(得分:2)
所有订购内容都需要operator==
和/或operator<
。它们可以实现为成员函数或全局函数。模板正在寻找类似的东西:
YourType const &lhs;
YourType const &rhs;
if (rhs < lhs) { ... }
对于Point
,您需要一个成员函数,如:
bool operator<(Point const &rhs) const
{ return x < rhs.x; }