简体   繁体   中英

Errors with generic singly linked list, C++

I tried creating a generic doubly linked list on my own, but got errors galore and couldn't understand why. I then turned to my book "Data structures and algorithms in c++", and tried to follow their implementation of a generic singly linked list. I wrote the code as it's presented in the book, but still got 40+ errors. My professor took a quick look at it in a break, but he couldn't find the problem either.

Been googling for hours and tried implementing the tips from here https://isocpp.org/wiki/faq/templates#class-templates For instance i've tried adding the line:

template class xxx<int>;

in the bottom of Snode.cpp and SLinkedList.cpp

The code below is the exact code from the book, with the exception of snode.cpp and the constructor and destructor in Snode.h, as they werent provided, and i had to "guess" how to implement them. I've tried just about any variation on them i could think of but nothing has resolved the problem

//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;
}

ERRORS:

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 ==========

Any help would be greatly appreciated

Lets take a closer look at these two specific lines of the error message:

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'

They say that the class SLinkedList has been declared as a non template class. And that it was done with the line

friend class SLinkedList<E>;

And that's correct. With the friend declaration you also declare the SLinkedList class (since it wasn't declared before). The problem is that you don't tell the compiler that it was a template.

You need to add a forward-declaration of the SLinkedList template first:

template<typename E>
class SLinkedList;

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

First:
Little misprint - in "SLinkedList.cpp" used SNode instead Snode.
Second:
If you want split file to *.h and .cpp , need to put #include "SLinkedList.cpp"
to end "SLinkedList.h" instead #include "SLinkedList.h" in begin "SLinkedList.cpp" .
Third:
E elem and Snode next in Snode.h declared as "private" - you can't have access in "SLinkedList::removeFront()".
Need access method or declare as "public".
Fourthly:
Can't compile template class to *.obj - look what is instantiation.
Need create object in some part of program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM