2010-09-20 71 views
1

錯誤C2664:「無效的std ::矢量< _Ty>: :的push_back(_Ty & &)「:不能從轉換參數1 '節點*' 到 '節點& &'錯誤C2664: '無效的std ::矢量<_Ty> ::的push_back(_Ty &&)':無法從 '節點<T> *' 轉換參數1至 '節點<T> &&'

請我需要幫助...

我創建node.h & heap.h

節點。 h:

#ifndef __NODE_H_ 
#define __NODE_H_ 
#include <string> 
#include <iostream> 
using namespace std; 

template <class T> 
class Node { 
private: 
    Node<T>* m_brother; 
    int m_index; 
    T m_data; 

public: 
    Node (T data); 
    ~Node(); 
    int GetIndex() const; 
    int GetBrother() const; 
    void SetIndex (const int index); 
    void SetBrother (const Node<T>* brother); 
    void SetData (const T& data); 
    bool operator<(const Node<T>& other) const; 
}; 

template <class T> 
Node<T>::Node(T data) { 
    SetData(data); 
} 

template <class T> 
int Node<T>::GetIndex() const { 
    return m_index; 
} 


template <class T> 
int Node<T>::GetBrother() const { 
    return m_brother->GetIndex(); 
} 

template <class T> 
void Node<T>::SetData (const T& data) { 
    m_data = data; 
} 

template <class T> 
void Node<T>::SetBrother(const Node<T>* brother) { 
    m_brother = brother; 
} 

template <class T> 
void Node<T>::SetIndex(const int index) { 
    if (index > 0) 
     m_index = index; 
    else 
     cout <<"ERROR: Index Can't be negative number!"<<endl; 
} 

template <class T> 
bool Node<T>:: operator<(const Node<T>& other)const 
{ 
    return *(this->GetData()) > *(other.GetData()); 
} 

#endif 



heap.h: 

#ifndef __HEAP_H_ 
#define __HEAP_H_ 
#pragma once 
#include <vector> 
#include "Node.h" 
using namespace std; 

template<class T> class Heap { 
public: 
    Heap(); 
    virtual ~Heap(); 
    Node<T> * CreateNode (T data); 
    bool IsEmpty() const; 
    Node<T>* RemoveNode(int indexNode); 
    Node<T>* ExtractMin(); 
    //void AddToHeap(Node<T>* newNode); 
    //void Add(int indexNode); 
    void Insert(Node<T>* newNode); 
    void DecreaseKey (Node<T>* newNode); 
    void Exchange (int indexNode1, int indexNode2); 
    void MinHeapify (int indexNode); 


private: 
    vector<Node<T>> m_heap; 
    int num; 
}; 


template<class T> 
Heap<T>::Heap() { 

} 

template<class T> 
Heap<T>::~Heap() { 
} 

template<class T> 
Node<T>* Heap<T>::CreateNode(T data) { 
    Node<T*>* node(T); 
    return node; 
} 

template<class T> 
bool Heap<T>::IsEmpty() const { 
    return (m_heap.size() == 0); 
} 

template<class T> 
Node<T>* Heap<T>::RemoveNode (int indexNum) { 
    Node<T>* nodeToRemove=NULL; 
    if (indexNum > 0 && indexNum < m_heap.size()) { 
    nodeToRemove = m_heap[indexNum]; 
    m_heap [indexNum] = m_heap [ m_heap.size()-1]; 
    m_heap [m_heap.size()-1] = nodeToRemove; 
    m_heap.pop_back(); 
    MinHeapify(nodeToRemove->GetIndex()); 
    } 
    return nodeToRemove; 
} 

template<class T> 
void Heap<T>::Insert(Node<T>* newNode) { 
    if (m_heap.size() == 0) { 
     m_heap.push_back(newNode); 
    } 
    else 
     DecreaseKey(newNode);  
} 

template<class T> 
void Heap<T>::DecreaseKey(Node<T>* newNode) { 
    m_heap.push_back(newNode); 
    int index = m_heap.size(); 
    while ((index > 0) && (m_heap[(index/2)-1] > m_heap[index-1])) { 
     Exchange(index,index/2); 
     index = index/2; 
    } 
} 

template<class T> 
Node<T>* Heap<T>::ExtractMin() { 
    Node<T>* minNode; 
    minNode = m_heap[0]; 
    m_heap[0] = m_heap[m_heap.size()-1]; 
    m_heap.erase(m_heap[m_heap.size()-1]); 
    MinHeapify (0); 
    return minNode; 
} 

template<class T> 
void Heap<T>::Exchange (int indexNode1, int indexNode2) { 
    Node<T>* tmp = m_heap[indexNode1-1]; 
    m_heap[indexNode1-1] = m_heap [indexNode2-1]; 
    m_heap[indexNode2-1] = tmp; 
} 


template<class T> 
void Heap<T>::MinHeapify (int indexNode) { 
    int leftNode = 2*indexNode; 
    int rightNode = 2*indexNode+1; 
    int smallest = indexNode; 
    if ((leftNode < m_heap.size()-1) && (m_heap[leftNode-1]<m_heap[smallest-1])) 
     smallest = leftNode; 
    if ((rightNode < m_heap.size()-1) && (m_heap[rightNode-1]<m_heap[smallest-1])) 
     smallest = rightNode; 
    if (smallest != indexNode) { 
     Exchange (indexNode,smallest); 
     MinHeapify(smallest); 
    } 
} 


#endif; 

在主,我試圖檢查,它沒有編譯。

int main() { 
Node<Vehicle*> a(car1); 
Heap<Vehicle*> heap; 
Node<Vehicle*>* p = &a; 
heap.Insert(p); 
return 0; 
} 

爲什麼?

+0

帶有雙下劃線的名稱(你的包含守衛)是[保留標識符](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac-標識符),請勿使用它們。 – GManNickG 2010-09-21 05:31:44

+0

@GMan - 更好的是,使用'#pragma once' – 2010-09-21 14:52:55

+0

@Steve:雖然這是實現定義的。 – GManNickG 2010-09-21 21:39:02

回答

4

您的Heap<T>::Insert函數需要Node<T*>*

m_heap定義爲vector<Node<T>>。您需要插入Node<T*>,而不是Node<T*>*Heap<T>::Insert應該通過const引用而不是通過指針來接受它的參數。

你的代碼不必要地使用了很多指針;如果您處理引用並按價值返回事物,而不是與遍佈各處的指針糾纏在一起,那將會簡單得多。

相關問題