错误:'需要模板参数;struct List'
error: template argument required for 'struct List'
我正在尝试为List类创建自己的模板作为学习练习。不过,我在模板语法方面遇到了一些问题,现在我收到了以下错误消息。。
main.cpp|Line 8|instantiated from here
error: template argument required for 'struct List'
In function 'int main()':
...
据我所知,我没有滥用任何东西,但这是我第一次使用模板,我真的很感激有人仔细检查,让我知道我做错了什么。
List.hpp:
#if !defined _LIST_HPP_
#define _LIST_HPP_
#include "Node.hpp"
///since we're creating a template everything must be defined in the hpp
template <typename ListType>
class List
{
public:
List();
bool Empty();
void PushFront();
void PushBack();
void PopBack();
Node<ListType>& GetHead();
private:
int _size;
Node<ListType>* _head;
Node<ListType>* _tail;
};
///implement List class here
template <typename ListType>
List<ListType>::List() : _head(0), _tail(0), _size(0)
{
}
template <typename ListType>
bool List<ListType>::Empty()
{
return _size == 0;
}
template <typename ListType>
void List<ListType>::PushFront()
{
_head = new Node<ListType>( _head , 0 );
if (!Empty())
_head->_prev->_next = _head; //set previous nodes _next to new _head
++_size;
}
template <typename ListType>
void List<ListType>::PushBack()
{
_tail = new Node<ListType>( 0 , _tail);
if (!Empty())
_tail->_next->_prev = _tail; // set old tails _prev to new tail
++_size;
}
template <typename ListType>
void List<ListType>::PopBack()
{
}
template <typename ListType>
Node<ListType>& List<ListType>::GetHead()
{
return _head;
}
#endif //define
Node.hpp:
#if !defined _NODE_HPP_
#define _NODE_HPP_
template<typename NodeType>
class Node{
public:
Node( Node* prev = 0, Node* next = 0);
void SetData(NodeType newData);
void GetData();
private:
friend class List;
NodeType _data;
Node* _next;
Node* _prev;
};
///implement Node
template <typename NodeType>
Node<NodeType>::Node(Node* prev, Node* next) : _prev(prev), _next(next)
{}
template <typename NodeType>
void Node<NodeType>::SetData(NodeType newData)
{
_data = newData;
}
template <typename NodeType>
void Node<NodeType>::GetData()
{
return _data;
}
#endif //define
主要.hpp
#include <iostream>
#include "List.hpp"
int main()
{
List<int> testl;
//test
testl.PushFront();
testl.GetHead().SetData(7); //Error thrown here??
std::cout << test1.GetHead().GetData() << std::endl;
return 0;
}
List
是一个类模板,因此您需要在朋友声明中声明它
template<typename ListType>
friend class List;
如果你只想让List<NodeType>
成为一个朋友,你需要告诉它这个模板参数,那么这个朋友声明就变成了
friend class List<NodeType>;
为了实现这一点,它需要知道List
作为类模板存在,因此您需要在Node.hpp
:的顶部转发声明它
template<typename ListType>
class List;
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- C++传递"struct"类型的参数作为引用 (&) 或按值传递
- 错误:'需要模板参数;struct List'
- 无法使用传递struct和enum作为参数的对象名
- 警告:'struct curl_fileinfo'参数列表中声明
- 将struct作为函数的参数是行不通的
- 通过struct将参数传递给CreateThread(),并在不影响前面线程的情况下更改后续线程的成员
- 如何使void*参数在函数中保存其局部结果,该函数使用struct *调用
- 函数参数中的struct关键字,以及const正确性
- 如何在c++类的构造函数中给出struct的向量作为参数?