【问题标题】:error: template argument required for 'struct List'错误:“结构列表”需要模板参数
【发布时间】:2012-03-24 10:03:27
【问题描述】:

我正在尝试为 List 类创建自己的模板作为学习练习。不过,我在模板语法方面遇到了一些问题,现在我收到以下错误消息..

main.cpp|Line 8|instantiated from here
error: template argument required for 'struct List'
In function 'int main()':
...

据我所知,我没有滥用任何东西,但这是我第一次使用模板,非常感谢有人仔细查看并让我知道我做错了什么。

列表.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

Main.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;
}

【问题讨论】:

    标签: c++ templates compiler-errors


    【解决方案1】:

    List 是一个类模板,所以你需要在你的朋友声明中这样声明它

    template<typename ListType>
    friend class List;
    

    如果你只想List&lt;NodeType&gt;成为朋友,你需要告诉它那个模板参数,那么朋友声明就变成了

    friend class List<NodeType>;
    

    为了让它工作,它需要知道List作为一个类模板存在,所以你需要在Node.hpp的顶部前向声明它:

    template<typename ListType>
    class List;
    

    【讨论】:

    • 啊,谢谢你,我需要很长时间才能弄清楚。你能扩展你的第三点吗?也许将其插入我的代码中?听起来很厚颜无耻,但我无法编译它。如果我将最后一段代码放在节点声明之外,则“朋友”语法不好,如果我将其放在节点类中,则对于 friend class List&lt;NodeType&gt;; 行,它会显示“列表不是模板”。你也确定上面的类型名是正确的吗?只是检查一下,就像我说的那样,我还不太熟悉模板语法。非常感谢您的回复!
    • 撒个谎,实际上只需进行上述template&lt;typename ListType&gt; friend class List; 所述的第一个更改即可使其工作......不介意关于帖子后半部分的更多信息,我遇到了麻烦,但否则我的程序可以正常工作:) 谢谢!!
    猜你喜欢
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多