c++ - 候选模板被忽略 : could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Point'

标签 c++ c++11

我在制作一个基本的 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> ,它说在包含的文件中。 我不知道这是否导致了问题。

提前致谢。

最佳答案

所有订购的东西都需要 operator==和/或 operator< .它们可以作为成员函数或全局函数来实现。模板正在寻找类似的东西:

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

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

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

关于c++ - 候选模板被忽略 : could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Point' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42660417/

相关文章:

c++ - Trie 实现运行时错误

c++ - 使用迭代器访问 vector<std::unique_ptr<T>> 的元素?

c++ - boost::lexical_cast 对于 c++11 stoi、stof 和 family 是否是多余的?

c++ - 使用声明的奇怪行为

c++ - 为什么扩展的 ASCII(特殊)字符需要 2 个字节才能存储?

C++从一个txt文件中读取并将 "|"之前的所有内容保存在缓冲区中

c++ - 在 operator+= 中使用 operator+ 比不使用它慢吗?

c++ - 基类中的非虚析构函数,但派生类中的虚析构函数导致段错误

c++ - Regex\w 在 C++11 中转义

c++ - 涉及通用引用的过载解决方案