【问题标题】:C++ Linked Lists Template ClassC++ 链表模板类
【发布时间】:2015-03-17 22:32:48
【问题描述】:

我目前正在练习我书中的一些链表作业,但我遇到了编译错误。实现文件似乎很好,但是头文件是接收错误的内容。

这是我的头文件:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <string>

using namespace std;

template<class T>
class linkedList {
public:
    linkedList();
    linkedList(const linkedList& copy);
    ~linkedList();
    int getSize() const;
    void addEntry(T entry);
    bool deleteEntry(T entry);
    T getEntry(int input) const;
    linkedList operator=(const linkedList& right);
private:
    struct node {
        T data;
        node<T> *next;
    };
    node<T> *linkedList = NULL;
};


#include "linkedlist.cpp"

#endif // LINKEDLIST_H

我从编译器得到的错误如下:

c:\users\andym_000\documents\linkedlist\linkedlist.h(22) : error C2059: syntax error : '<'
c:\users\andym_000\documents\linkedlist\linkedlist.h(20) : see reference to class template instantiation 'linkedList<T>::node' being compiled
c:\users\andym_000\documents\linkedlist\linkedlist.h(25) : see reference to class template instantiation 'linkedList<T>' being compiled
c:\users\andym_000\documents\linkedlist\linkedlist.h(22) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedlist.h(24) : error C2059: syntax error : '<'
c:\users\andym_000\documents\linkedlist\linkedlist.h(24) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2059: syntax error : '<'
        ..\linkedList\main.cpp(9) : see reference to class template instantiation 'linkedList<T>' being compiled
        with
        [
            T=std::string
        ]
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2059: syntax error : '<'
        ..\linkedList\main.cpp(10) : see reference to class template instantiation 'linkedList<T>' being compiled
        with
        [
            T=int
        ]
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2238: unexpected token(s) preceding ';'

【问题讨论】:

  • 你搞砸了你的答案,现在你有错误的代码是不存在的。您应该扩展问题而不是更改,发布另一个问题或使用比 SO 更好的地方来获得此类帮助。
  • 可能问题在于node *linkedList = NULL;,这在 C++03 中是不合法的。省略 `=NULL` 的东西,它既不必要又非法。类中的所有内容都是使用默认构造函数构造的(除非另有明确说明),并且指针初始化为 0 (NULL)。
  • 您必须将问题恢复为原始形式。你可以找到它here,这样你就不用再写了。如果您不自己解决,我将不得不标记您的问题以引起版主的注意。

标签: c++ templates linked-list nodes


【解决方案1】:

嗯,struct node {...} 不是模板。你的错误告诉你不知道如何处理node&lt;T&gt;

【讨论】:

  • 啊,我明白了,所以你的意思是我需要做模板 struct node { ... }
  • @andayn 不,只是struct node { }。类模板中的嵌套类自动具有T 可用。
  • 另外,您可以在模板中使用模板,但您必须指定新模板。这就是 node 会抛出错误的原因。 p.s.像你一样改变你的问题会让人们误解答案的意思。
【解决方案2】:

您将模板表示法与node 类一起使用,这不是模板。 T 类型是为整个 linkedList 定义的 - 在 node 的定义中,它是明确定义的类型。

请注意,node 是在 linkedList 中定义的,因此对于 linkedList&lt;T&gt;,它的限定名称将是 linkedList&lt;T&gt;::node(即 linkedList 的每个实例都有自己的 node 类)。

所以,只需将 node&lt;T&gt; 替换为 node 即可。

【讨论】:

  • 谢谢你的回复,现在按照你说的去做,我只有三个错误,都说构造函数语法缺少正式的边界和头文件中的编译器标志node *linkedList;。我又一次不知所措了。
  • @andayn SO 不是增量调试/支持的地方。你试过聊天室吗?
  • @andayn 似乎是特定 T 类型的问题。您的实现需要它来实现默认 (T()) 和复制 (T(const T&amp;)) 构造函数,可能提供 T 没有它们。仅此标头不会产生任何错误,并且正确实现应该适用于大多数 T 类型(如 int)。
猜你喜欢
  • 2011-01-05
  • 2010-12-16
  • 2012-06-21
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
相关资源
最近更新 更多