2017-03-07 98 views
0

我遇到了一个基本的cpp程序问题。我正在尝试为该程序编写基础知识。候选模板被忽略:无法匹配'pair <type-parameter-0-0,type-parameter-0-1>'对'const'点'

#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>,它说,在文件来自包括在内。 我不知道这是否导致问题。

在此先感谢。

+2

“*下令对X优先级队列坐标*”你觉得会发生什么? – juanchopanza

回答

2

所有订购的东西都需要operator==和/或operator<。它们可以作为成员函数或全局函数来实现。这些模板看起来像什么事:

YourType const &lhs; 
YourType const &rhs; 
if (rhs < lhs) { ... } 

对于Point你需要像一个成员函数:

bool operator<(Point const &rhs) const 
{ return x < rhs.x; }