错误:原型与类

时间:2017-03-20 05:17:23

标签: c++ class templates prototype

我在尝试编译代码时收到此错误:

Priority_queue.h:63:6: error: prototype for ‘void Priority_queue<T>::push_heap(Iterator, Iterator)’ does not match any in class ‘Priority_queue<T>’
 void Priority_queue<T>::push_heap(Iterator start, Iterator stop) {
      ^
Priority_queue.h:31:10: error: candidate is: void Priority_queue<T>::push_heap(typename std::vector<T>::iterator, typename std::vector<T>::iterator)
     void push_heap(typename vector<T>::iterator start, typename vector<T>::iterator stop);
          ^

Priority_queue.h

#include <vector>
using namespace std;

template <class T>
class Priority_queue {
public:
    ...

    void push_heap(typename vector<T>::iterator start, typename vector<T>::iterator stop);
    void pop_heap(typename vector<T>::iterator start, typename vector<T>::iterator stop);
    void adjust_heap(typename vector<T>::iterator start, unsigned int heapSize, unsigned int position);
    void make_heap(typename vector<T>::iterator start, typename vector<T>::iterator stop);
    void sort_heap(typename vector<T>::iterator start, typename vector<T>::iterator stop);
    void heap_sort(typename vector<T>::iterator start, typename vector<T>::iterator stop);
private:
    vector<T> c;
};

template <class T>
template <class Iterator>
void Priority_queue<T>::push_heap(Iterator start, Iterator stop) {
...
}

为什么这不起作用?我也为其余的函数得到了相同类型的错误。

1 个答案:

答案 0 :(得分:1)

错误消息很明确; decalration和定义不匹配。 push_heap被声明为类模板Priority_queue的成员函数,但被定义为类模板Priority_queue的成员函数模板

如何解决这个问题取决于你原来的意图;您可以将声明更改为

template <class T>
class Priority_queue {
public:
    ...
    template <class Iterator>
    void push_heap(Iterator start, Iterator stop);
    ...
};

LIVE1

或将定义更改为

template <class T>
void Priority_queue<T>::push_heap(typename vector<T>::iterator start, typename vector<T>::iterator stop) {
...
}

LIVE2