【问题标题】:Errors with generic singly linked list, C++通用单链表的错误,C++
【发布时间】:2019-02-15 12:40:54
【问题描述】:

我尝试自己创建一个通用的双向链表,但出现大量错误并且无法理解原因。然后我转向我的书“C++ 中的数据结构和算法”,并尝试遵循他们对通用单链表的实现。我按照书中介绍的方式编写了代码,但仍然出现 40 多个错误。我的教授在休息的时候快速浏览了一下,但他也找不到问题。

在谷歌上搜索了几个小时,并尝试从这里https://isocpp.org/wiki/faq/templates#class-templates 实施提示 例如,我尝试添加以下行:

template class xxx<int>;

在 Snode.cpp 和 SLinkedList.cpp 的底部

下面的代码是书中的确切代码,除了 snode.cpp 以及 Snode.h 中的构造函数和析构函数,因为它们没有提供,我不得不“猜测”如何实现它们。我已经尝试了我能想到的任何变化,但没有解决问题

//Snode.h

#pragma once
#include <string>
#include <iostream>

using namespace std;

template <typename E>
class Snode
{
public:
Snode();
~Snode();
private:
E elem;
Snode<E>* next;
friend class SLinkedList<E>;
};

Snode.cpp

#include "Snode.h"
template <typename E>
Snode<E>::Snode()
{
}

template <typename E>
Snode<E>::~Snode()
{
}

SLinkedList.h

#pragma once
#include "Snode.h"

template<typename E>
class SLinkedList
{
public:
    SLinkedList();
    ~SLinkedList();
    bool empty() const;
    const E& front() const;
    void addFront(const E& e);
    void removeFront();
private:
    Snode<E>* head;
};

SLinkedList.cpp

#include "SLinkedList.h"


template <typename E>
SLinkedList<E>::SLinkedList():
head(nullptr){}

template <typename E>
SLinkedList<E>::~SLinkedList()
{
    while (!empty()) removeFront();)
}

template <typename E>
bool SLinkedList<E>::empty() const
{
    return head == nullptr;
}

template <typename E>
const E& SLinkedList<E>::front() const
{
    return head->elem;
}

template <typename E>
void SLinkedList<E>::addFront(const E& e)
{
    SNode<E>* v = new SNode<E>;
    v->elem = e;
    v->next = head;
    head = v;
}

template <typename E>
void SLinkedList<E>::removeFront()
{
    SNode<E>* old = head;
    head = old->next;
    delete old;
}

错误:

1>------ Build started: Project: Generic singly linked list, Configuration: Debug Win32 ------
1>SLinkedList.cpp
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(16): error C2059: syntax error: '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(17): note: see reference to class template instantiation 'Snode<E>' being compiled
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(16): error C2238: unexpected token(s) preceding ';'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.h(16): error C2989: 'SLinkedList': class template has already been declared as a non-class template
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(16): note: see declaration of 'SLinkedList'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.h(4): error C3857: 'SLinkedList': multiple template parameter lists are not allowed
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(5): error C2988: unrecognizable template declaration/definition
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(5): error C2059: syntax error: '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(6): error C2448: 'head': function-style initializer appears to be a function definition
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(9): error C2988: unrecognizable template declaration/definition
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(9): error C2059: syntax error: '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(9): error C2523: '::~SLinkedList': destructor tag mismatch
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(10): error C2143: syntax error: missing ';' before '{'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(10): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(15): error C2988: unrecognizable template declaration/definition
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(15): error C2143: syntax error: missing ';' before '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(15): error C2059: syntax error: '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(16): error C2143: syntax error: missing ';' before '{'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(16): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(21): error C2988: unrecognizable template declaration/definition
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(21): error C2143: syntax error: missing ';' before '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(21): error C2040: 'SLinkedList': 'const E &' differs in levels of indirection from 'bool'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(21): error C2059: syntax error: '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(21): error C2039: 'front': is not a member of '`global namespace''
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(22): error C2143: syntax error: missing ';' before '{'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(22): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(27): error C2988: unrecognizable template declaration/definition
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(27): error C2143: syntax error: missing ';' before '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(27): error C2182: 'SLinkedList': illegal use of type 'void'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(27): error C2371: 'SLinkedList': redefinition; different basic types
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(15): note: see declaration of 'SLinkedList'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(27): error C2059: syntax error: '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(27): error C2039: 'addFront': is not a member of '`global namespace''
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(28): error C2143: syntax error: missing ';' before '{'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(28): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(36): error C2988: unrecognizable template declaration/definition
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(36): error C2143: syntax error: missing ';' before '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(36): error C2182: 'SLinkedList': illegal use of type 'void'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(36): error C2371: 'SLinkedList': redefinition; different basic types
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(15): note: see declaration of 'SLinkedList'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(36): error C2059: syntax error: '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(36): error C2039: 'removeFront': is not a member of '`global namespace''
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(37): error C2143: syntax error: missing ';' before '{'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.cpp(37): error C2447: '{': missing function header (old-style formal list?)
1>Snode.cpp
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(16): error C2059: syntax error: '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(17): note: see reference to class template instantiation 'Snode<E>' being compiled
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(16): error C2238: unexpected token(s) preceding ';'
1>Source.cpp
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(16): error C2059: syntax error: '<'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(17): note: see reference to class template instantiation 'Snode<E>' being compiled
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(16): error C2238: unexpected token(s) preceding ';'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.h(16): error C2989: 'SLinkedList': class template has already been declared as a non-class template
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(16): note: see declaration of 'SLinkedList'
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.h(4): error C3857: 'SLinkedList': multiple template parameter lists are not allowed
1>Generating Code...
1>Done building project "Generic singly linked list.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

任何帮助将不胜感激

【问题讨论】:

  • 我撤回了我的标志,因为这不是真正的问题(如答案中所述),但我会在此处留下链接,因为这是您将遇到的下一个问题。
  • 谢谢。是的,在我写的原始帖子中,我在底部添加了“template void ..”这一行,但这是一个错字,我的意思是“template class S ...”,这也解决了这个问题。

标签: c++ templates singly-linked-list


【解决方案1】:

让我们仔细看看错误消息的这两行:

1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\slinkedlist.h(16): error C2989: 'SLinkedList': class template has already been declared as a non-class template
1>c:\users\kent\source\repos\generic singly linked list\generic singly linked list\snode.h(16): note: see declaration of 'SLinkedList'

他们说SLinkedList 类已被声明为 模板类。并且它是用这条线完成的

friend class SLinkedList<E>;

这是正确的。使用friend 声明,您还声明 SLinkedList 类(因为它之前没有声明过)。问题是你没有告诉编译器它是一个模板。

您需要先添加SLinkedList的前向声明模板

template<typename E>
class SLinkedList;

template<typename E>
class SNode
{
    ...
};

【讨论】:

  • 漂亮!非常感谢。在修正了一些错别字并在 .cpp 文件中添加了“模板类 S....”之后,它就像一个魅力!
【解决方案2】:

第一:
小错误 - 在“SLinkedList.cpp”中使用 SNode 而不是 Snode。
第二:
如果要将文件拆分为 *.h 和 .cpp ,需要输入 #include "SLinkedList.cpp"
结束 "SLinkedList.h" 而不是 #include "SLinkedList.h" in begin "SLinkedList.cpp" 。
第三:
E elem 和 Snode
next 在 Snode.h 中声明为“私有” - 你不能访问 在“SLinkedList::removeFront()”中。
需要访问方法或声明为“公共”。
第四:
无法将模板类编译为 *.obj - 看看什么是实例化。
需要在程序的某些部分创建对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多