2017-03-07 95 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; }