-4

Hello I am working on a project for data structures and I am stuck. The project is to build a templated linked list class and a nested iterator class, inside a headerfile only. I do not understand how to use the iterator class inside of the templated LinkedList class. I am not sure about iterators in general. The methods declared are all needed to run the tests that will be ran. I am currently stuck on initializing 'LinkedList* prev' and next in the constructor. Please help! How do I initialize something that is templated? the compiler wants to know what data type I think? Thanks for your time The error is on the constructor declaration in the class LinkedList EDITED "invalid initialization of non-const reference of type 'std::basic_string char&' from an rvalue of type 'int'" is the error message

#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
template <typename T> class  LinkedList
{

    private:
        LinkedList<T>* head;
        int size;
        T element = NULL;
        LinkedList<T>* prev;
        LinkedList<T>* next;
    public:


        class Iterator
        {
                friend class LinkedList<T>;
            private:

            public:
                Iterator();
                T operator*() const;
                Iterator& operator++();
                bool operator==(Iterator const& rhs);
                bool operator!=(Iterator const& rhs);
        };

        LinkedList<T>():prev(0), next(0){} //HERE is the ERROR????////////
        Iterator begin() const
        {
 //         Iterator tempIterator = LinkedList<T>();
//
//          return tempIterator->front;
            return NULL;
        }
        Iterator end() const{return NULL;}
        bool isEmpty() const;
        T getFront() const;
        T getBack() const;
        void enqueue(T newElement)
        {
            LinkedList<T>* newElem = new LinkedList<T>();
            newElem->element = newElement;
            if(front == 0)
            {
                front = newElem;
            }
            else
            {
                 back->next = newElem;
            }
            back = newElem;
            listSize++;
         }
        void dequeue()
        {
            LinkedList<T>* tempElem = new LinkedList<T>();
            if(front == 0){
                cout << "List is empty!" << endl;
            }
            else
            {
                tempElem = front;
                front = front->next;
                cout << "the element dequeued: " << tempElem->element;
                delete tempElem;
            }
        }
        void pop();
        void clear();
        bool contains(T element) const;
        void remove(T element);

    };





#endif /* LINKEDLIST_H_ */
gtbrew
  • 1
  • 3
  • 1
    Post a [MCVE] that reproduces the error. Post error messages verbatim please. – πάντα ῥεῖ Sep 11 '16 at 01:02
  • `front`, `back` and `listSize` are not declared. Where do they come from? –  Sep 11 '16 at 01:09
  • 2
    If your instructor told you that it's a good idea to `use namespace std;`, and even better idea to stick that into a header file, then you need to [immediately find a better instructor](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). You're being taught bad programming habits that will only work against you, if you truly intend to have a career in computer programming. – Sam Varshavchik Sep 11 '16 at 01:09
  • Assuming this is just for academic instruction (because you'd be insane not to use `std::list`, `std::deque`, or `std::vector` if it isn't), do yourself a favor and create a `Node` nested class inside `LinkedList`. A chain of *those* is what will hold your elements and their fore-and-aft pointers. nested like your iterator class. The `LinkedList` itself just contains the head pointer, the tail pointer, some sizing info, etc. As is now, each node in the list is carrying around it's own *head* pointer, which doesn't really make a lot of sense. – WhozCraig Sep 11 '16 at 01:17
  • **WHAT** error? Where is the error message you're asking about? – user207421 Sep 11 '16 at 01:38
  • i will change using namespace std thank you, "invalid initialization of non-const reference of type 'std::basic_string char&' from an rvalue of type 'int'" is the error message, I left out what I know how to fix. I am an online student, this is my second time using this. – gtbrew Sep 11 '16 at 01:54
  • Thanks WhozCraig, I was thinking node struct, but a class? – gtbrew Sep 11 '16 at 01:55

1 Answers1

2

The constructor syntax is wrong.

LinkedList():prev(0), next(0){}

This is the correct constructor syntax. Once the template class and parameters are declared, in the template declaration, the name of the template alone, without the parameters, refers to the template itself, inside its scope. Similarly, if you were to explicitly define a destructor, it would be:

~LinkedList() { /* ... */ }

If you wanted to only declare the constructor inside the template declaration:

 LinkedList();

and then define it outside of the template declaration, then the correct syntax would be:

template<typename T>
LinkedList<T>::LinkedList() :prev(0), next(0){}
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Are you sure this is the issue here? At least gcc does not complain about either `LinkedList()` or `LinkedList()` and I think the standard requires in §12.1.1 only a class-name referring to the current istantiation, which by §14.6.2.1.1 may include the template argument list. I am not very familiar with the standard though. –  Sep 11 '16 at 01:33
  • thank you for your answer. the error is "invalid initialization of non-const reference of type 'std::basic_string char&' from an rvalue of type 'int' – gtbrew Sep 11 '16 at 01:51
  • Constructor works now, thanks again! you rock – gtbrew Sep 11 '16 at 02:09
  • I added a node class as suggested above, it contains T element, LinkedList prev, and LinkedList next, how could I create a constructor the way you suggested? I tried just a Node constructor inside of the node class, then I tried template LinkedList::Node() : element(), prev(0), next(0) , I also tried ...Node::Node()... used the same way – gtbrew Sep 11 '16 at 02:36