gpt4 book ai didi

c++ - 与创建模板类相关的错误

转载 作者:行者123 更新时间:2023-11-28 02:40:24 27 4
gpt4 key购买 nike

我正在创建一个包含内部 friend Node 类的 PriorityQueue 模板类。当我尝试编译以下代码时,我现在收到这些错误:

    PriorityQueue.h:20:12: error: declaration of ‘class T’
template<typename T> class Node
^
PriorityQueue.h:16:11: error: shadows template parm ‘class T’
template <typename T> class PriorityQueue
^
PriorityQueue.h:22:25: error: expected nested-name-specifier
friend typename PriorityQueue<T>;
^
PriorityQueue.h:22:41: error: invalid declarator before ‘;’ token
friend typename PriorityQueue<T>;
^
PriorityQueue.cpp:66:1: error: ‘PriorityQueue<T>::Node’ is not a type
PriorityQueue<T>::Node::Node()
^
PriorityQueue.cpp:66:30: error: ISO C++ forbids declaration of ‘Node’ with no type [-fpermissive]
PriorityQueue<T>::Node::Node()
^
PriorityQueue.cpp:66:30: error: invalid use of dependent type ‘typename PriorityQueue<T>::Node’

PriorityQueue.cpp:74:1: error: ‘PriorityQueue<T>::Node’ is not a type
PriorityQueue<T>::Node::Node(T data, int *index, float priority)
^

PriorityQueue.cpp:74:64:错误:ISO C++ 禁止声明没有类型的“节点”[-fpermissive] PriorityQueue::Node::Node(T data, int *index, float priority) ^ PriorityQueue.cpp:74:64: 错误:无效使用依赖类型‘typename PriorityQueue::Node’

优先队列.h:

#ifndef PRIORITYQUEUE_H_
#define PRIORITYQUEUE_H_
#include <vector>
#include <map>

using namespace std;

template<typename T> class Node

template <typename T> class PriorityQueue
{

public:
template<typename T> class Node
{
friend typename PriorityQueue<T>;
private:
T data;
int *index;
float priority;

public:
Node();
Node(T data, int *index, float priority);
};

private:
vector<T> v;
map<T, int*> m;
int vSize;
public:
PriorityQueue();
~PriorityQueue();
void insert(T obj, float priority);
T front();
T pop();
bool isEmpty();
void changePriority(T obj, float new_priority);

};

#endif

优先队列.cpp:

#include "PriorityQueue.h"
#include <vector>
#include <map>

using namespace std;

template<class T>
PriorityQueue<T>::PriorityQueue()
{
vector<T> v;
map<T, int*> m;
this.vSize = 0;
}
template<class T>
PriorityQueue<T>::~PriorityQueue()
{

}
template<class T>
void PriorityQueue<T>::insert(T obj, float priority)
{
Node<T> n = new Node<T>(obj, vSize, priority);

v[vSize] = n;
m.insert(obj, vSize);

vSize++;
}
template<class T>
T PriorityQueue<T>::front()
{

}
template<class T>
T PriorityQueue<T>::pop()
{

}
template<class T>
bool PriorityQueue<T>::isEmpty()
{
bool b = false;
if(vSize > 0)
{
b = true;
return b;
}
else
{
return b;
}
}
template<class T>
void PriorityQueue<T>::changePriority(T obj, float new_priority)
{

}

template<class T>
PriorityQueue<T>::Node::Node()
{
data = NULL;
index = 0;
priority = 0.0;
}

template<class T>
PriorityQueue<T>::Node::Node(T data, int *index, float priority)
{
this->data = data;
this->index = index;
this->priority = priority;
}

如有任何帮助,我们将不胜感激。

最佳答案

看起来您在 Node 的前向声明之后缺少分号。这可能是编译器头痛的根源。

关于c++ - 与创建模板类相关的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26172919/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com