简体   繁体   English

错误C2664:&#39;void std :: vector &lt;_Ty&gt; :: push_back(_Ty &amp;&amp;)&#39;:无法从&#39;Node转换参数1 <T> *&#39;到&#39;节点 <T> &amp;&amp;”

[英]error C2664 : 'void std::vector<_Ty>::push_back(_Ty&&)': cannot convert parameter 1 from 'Node<T> *' to 'Node<T>&&'

error C2664 : 'void std::vector<_Ty>::push_back(_Ty&&)': cannot convert parameter 1 from 'Node *' to 'Node&&' 错误C2664:'void std :: vector <_Ty> :: push_back(_Ty &&)':无法将参数1从'Node *'转换为'Node &&'

please I need help... 拜托,我需要帮助......

I created node.h & heap.h 我创建了node.h和heap.h

node.h : node.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;

In the main, I tried to check and it didn't compiled. 在主要,我试图检查,它没有编译。

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

why? 为什么?

Your Heap<T>::Insert function takes a Node<T*>* . 您的Heap<T>::Insert函数采用Node<T*>*

m_heap is defined as a vector<Node<T>> . m_heap定义为vector<Node<T>> You need to insert a Node<T*> , not a Node<T*>* . 您需要插入Node<T*> ,而不是Node<T*>* Heap<T>::Insert should take its parameter by const reference, not by pointer. Heap<T>::Insert应该通过const引用而不是指针来获取其参数。

Your code uses a lot of pointers unnecessarily; 你的代码不必要地使用了许多指针; it would be much simpler if you dealt with references and returned things by value rather than tangling with pointers all over the place. 如果你处理引用并按值返回东西而不是在整个地方纠缠指针,这会简单得多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 接收错误C2664:传递向量时,无法将参数1从&#39;std :: vector &lt;_Ty&gt;&#39;转换为&#39;std :: vector &lt;_Ty,_Ax&gt;&&#39; <int> 模板法 - Receiving error C2664: cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::vector<_Ty,_Ax> &' when passing vector<int> to templated method &#39;std :: list &lt;_Ty&gt; :: push_back&#39;:无法将&#39;this&#39;指针从&#39;const std :: list &lt;_Ty&gt;&#39;转换为&#39;std :: list &lt;_Ty&gt;&&#39; - 'std::list<_Ty>::push_back' : cannot convert 'this' pointer from 'const std::list<_Ty>' to 'std::list<_Ty> &' 错误C2664&#39;无效IVerify :: SetParams(void)&#39;:无法将参数1从&#39;std :: wstring&#39;转换为&#39;wchar_t *&#39; - Error C2664 'void IVerify::SetParams(void)': cannot convert argument 1 from 'std::wstring' to 'wchar_t *' 无法从&#39;std :: vector &lt;_Ty&gt;&#39;转换为&#39;std :: vector &lt;_Ty&gt;& - cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty> & 无法将参数3从&#39;std :: vector &lt;_Ty&gt; *&#39;转换为&#39;库存*&#39;。 为什么? - cannot convert parameter 3 from 'std::vector<_Ty> *' to 'Inventory *'. Why? “MessageBoxA”:无法将参数 2 从“std::vector<_Ty>”转换为“LPCSTR” - 'MessageBoxA' : cannot convert parameter 2 from 'std::vector<_Ty>' to 'LPCSTR' c2664无法将参数2从&#39;std :: string&#39;转换为&#39;const void *&#39; - c2664 cannot convert parameter 2 from 'std::string' to 'const void *' 错误 C2664:“wcscmp”:无法将参数 1 从“CHAR [260]”转换为“const wchar_t *” - error C2664: 'wcscmp' : cannot convert parameter 1 from 'CHAR [260]' to 'const wchar_t *' 错误:C2664:&#39;QXmlStreamWriter :: writeAttributes&#39;:无法从&#39;QVector转换参数1 <T> &#39;至&#39;const QXmlStreamAttributes&&#39; - error: C2664: 'QXmlStreamWriter::writeAttributes' : cannot convert parameter 1 from 'QVector<T>' to 'const QXmlStreamAttributes &' 错误C2440:&#39;初始化&#39;:无法从&#39;初始化列表&#39;转换为&#39;std :: vector <char *,std::allocator<_Ty> &gt;” - error C2440: 'initializing': cannot convert from 'initializer list' to 'std::vector<char *,std::allocator<_Ty>>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM