0

I am trying to declare a iterator class within an array class and then implement it outside the array class. However, I am encountering a syntax error. I am not sure why this is not working. I have only been able to deduce that the problem is with my attempt to implement my array iterator. I have labeled the line of the given error below. Ask if you need any other code to be posted or some sort of explanation, I removed a lot of code to prevent posting a mess.

EDIT: I replaced the wrong header with the correct header. I had received different errors which I fixed by placing typename keyword. Is it possible for someone to explain why the typename keyword was my iterator classes implementation? The error I received without it was

std_bidirectional_iterator: ds::Array::iterator is not a valid template argument for parameter derived.

Array.h

#include "Common.h"
#include "ArrayInterface.h"
#include "std_bidirectional_iterator.h"

template<class T, size_t N>
class ds::ArrayTwo : public ArrayInterface<T>
{
    class iterator; // Tried with all following implementations
    template<class T> class reverse_iterator;

    template<class U> class const_iterator;
    template<class T> class const_reverse_iterator;
};

template<class T, size_t N>
// Error on line below[fixed by typename]
class ds::ArrayTwo<T, N>::iterator : public std_bidirectional_iterator<typename ds::ArrayTwo<T, N>::iterator, T>
{
};

std_bidirectional_iterator.h

#include <iterator>

template<class Derived, class Type>
class std_bidirectional_iterator : public std::iterator<std::bidirectional_iterator_tag, Type>
{
    virtual Derived & operator=(const Derived & it) = 0;            // Copy Assignment Operator

    virtual reference operator*(void) = 0;                          // De-Reference Operator (Returns reference)

    virtual pointer operator->(void) = 0;                           // De-Reference Operator (Returns pointer)

    virtual Derived & operator++(void) = 0;                         // Pre-Fix ++
    virtual Derived operator++(int) = 0;                            // Post-Fix ++

    virtual Derived & operator--(void) = 0;                         // Pre-Fix --
    virtual Derived operator--(int) = 0;                            // Post-Fix --

    virtual bool operator==(const Derived & it) const = 0;          // Equal To Operator
    virtual bool operator!=(const Derived & it) const = 0;          // Not Equal To Operator
};
Freddy-FazBear
  • 195
  • 2
  • 3
  • 13
  • 1
    A simplified version [compiles for me](https://rextester.com/WBUO56976). Show a [mcve] – Igor Tandetnik Jan 20 '19 at 22:02
  • 1
    One tweak was necessary to [get it compile with MSVC](https://rextester.com/LKNB62126) - make it `public std_bidirectional_iterator::iterator, T>`. Note the `typename` keyword. – Igor Tandetnik Jan 20 '19 at 22:06
  • 1
    Questions about error messages should always include the error message, in its entirety, copy and pasted rather than typed if at all possible. It should virtually always be possible for coding questions (that is, anything on topic for stack overflow) – Ed Grimm Jan 20 '19 at 22:28
  • @ Igor Tandetnik I went to look through my code again to replicate the error using as much code as possible when i discovered I included the wrong header file(I am disappointed in myself). However, after I fixed that I had received more errors which I fixed by placing the typename keyword as you suggested. It works now. I was wondering, If you would be able to post a answer to this question and explain the purpose of the typename keyword in this specific case? I will gladly mark it as the solution. Thanks for your help. – Freddy-FazBear Jan 20 '19 at 22:52
  • 1
    [Where and why do I have to put the “template” and “typename” keywords?](https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – Igor Tandetnik Jan 21 '19 at 00:32

0 Answers0