【问题标题】:C++ template class with template class member具有模板类成员的 C++ 模板类
【发布时间】:2013-01-17 22:35:40
【问题描述】:

在模板类中编写模板成员类参数的当前语法是什么。

这是我一直在尝试做的:

template <class T>
    class Node
    {
    public:
        Node(); // constructor 
        Node(const Node<T> &);           // copy constructor 
        ~Node();             // destructor
        T value;
        Node *next;
    };


template <class T> 
class Linked_list
{
public:
    Linked_list(); // constructor 
    Linked_list(const Linked_list<T> &); // copy constructor 
    ~Linked_list();             // destructor
    T pop();
    void push(T value);
    T top();
    bool is_empty();
    void clear();

private:
    Node<T> *head; // COMPILER ERROR
};

为什么这是编译器错误?

Node<T> *head; // COMPILER ERROR

【问题讨论】:

  • 不是。 GCC 用-ansi -Wall -Wextra -pedantic 编译它就好了。
  • “医生,我病了”...“告诉我你的症状,然后”...“我病了!!!” tinyurl.com/so-hints
  • Node 和 Linked_list 都在一个源文件中?
  • @SirDarius 大声笑,它说“语法错误:缺少';'在'之前
  • 可能是这个问题,给我们看看真正的源文件结构

标签: c++ class templates


【解决方案1】:

也许当你打电话时:

Node<T> *head;

T 不是对象类型,它不知道如何构造它。试试:

Node<std::string> *head;

或类似的东西。 T 不是对象类型,它就像一个变量名,只不过它实际上是类 Node 和 LinkedList 中的一个变量类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-11
    • 2017-02-19
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多