0

I am trying to create an unordered Linked List and the textbook that I am using says that a linked list iterator class is needed before creating the linked list ADT. Ultimately it will be a functional linked list that can print in reverse, split at a node and merge with another. I have only started the linked List iterator class and the code has been copied from the text, yet does not build. I assume this is a header / forward declaration issue yet I have tried that and still nothing. Any ideas?

//.h file - linkedListIterator.h

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cstddef>

using namespace std;

#ifndef LINKEDLISTITERATOR_H
#define LINKEDLISTITERATOR_H

template <class Type>
class linkedListIterator
{
    public:
        //Default COnstructor
        linkedListIterator();

    //Constructor with parameter, *curretn = *ptr
    linkedListIterator(nodeType<Type> *ptr);

    //overload deref operator - return info in node
    Type operator*();

    //Overlaod pre inc operator - adv iterator to next node
    linkedListIterator<Type> operator++;

    //Overload equlaity operator - return true if iterator is eq to iter on right
    bool operator==(const linkedListIterator<Type>& right) const;

    //Overload not equal to operator- return true iter is not eq to iter on right
    bool operator!=(const linkedListIterator<Type>& right) const;

private:
    //ptr to current node in linked list
    nodeType<Type> *current;
};

#endif

//.cpp file - linkedListIterator.cpp

#include "stdafx.h"
#include "linkedListIterator.h"

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cstddef>

using namespace std;

template <class Type>
linkedListIterator<Type>::linkedListIterator()
{
    current = nullptr;
}

template <class Type>
linkedListIterator<Type>::linkedListIterator(nodeType<Type> *ptr)
{
    current = ptr;
}

template <class Type>
Type linkedListIterator::operator*()
{
    return current->info;
}

template <class Type>
linkedListIterator<Type> linkedListIterator<Type>::operator++()
{
   current = current->link;
   return *this;
}

template <class Type>
bool linkedListIterator<Type>::operator==(const linkedListIterator<Type>& right) const
{
    return (current == right.current);
}

template <class Type>
bool linkedListIterator<Type>::operator==(const linkedListIterator<Type>& right) const
{
    return (current != right.current);
}

//main file - CH16_Q4Q6Q7_OrderedandUnordered.cpp
/nothing here yet/

#include "stdafx.h"

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cstddef>

using namespace std;

int main()
{
    return 0;
}

I am receiving errors
C2061 - syntax error: identifier 'nodeType'
C2535 - 'linkedListIterator::linkedListIterator(void)': member function already defined or declared'
C2143 - syntax error: missing ';' before '<'
C4430 - missing type specifier - int assumed. Note C++ does not support default-int
C2238 - unexpected token(s) preceding';'
C2988 - unrecognizable template declaration / definition
C2059 - syntax error '('
C2649 - 'typename': is not a 'class'
C1004 - unexpected end-of-file found

mNDD
  • 1
  • 1. What's in your `stdafx.h`? 2. It seems that `nodeType` is not defined anywhere, which is what the first error is complaining about. 3. [You cannot implement templates in .cpp files](http://stackoverflow.com/q/495021/1782465). – Angew is no longer proud of SO Jan 03 '17 at 16:55
  • Thank you all, it seems that you can't put the template definition in .cpp but it must be in the .h – mNDD Jan 03 '17 at 22:30
  • Yes, you can't do that. But moving it to `.h` files will not solve the fact that you're using `nodeType` which is not defined anywhere. – Angew is no longer proud of SO Jan 04 '17 at 07:33

0 Answers0