0

I have one class:

template <class EventType = SFAEvent>
class SFAEventDispatcher {
    SFALinkedList<SFACallback<EventType> > callbacks[size];

    void dispatch(EventType &e) {
        ...
        SFALinkedList<SFACallback<EventType> >::iterator iter = callbacks[e.type].begin;
        ...
    };

}

I'm using a LinkedList with iterator defined as:

template <typename Type>
class SFALinkedList {
public: 
    typedef SFALinkedListIterator<SFASinglyElement<Type> > iterator;
}

The problem is:

If I use:

SFALinkedList<SFACallback<EventType> >::iterator iter;

EventType defined in template, don't work, but if I use:

SFALinkedList<SFACallback<SFAEvent> >::iterator;

SFAEvent being a class, work;

Example of my Linked List:

SFALinkedList<float> list;
list.append(1.1);
list.append(2.2);
list.append(3.3);
list.append(4.4);

for(SFALinkedList<float>::iterator inter = list.begin(); inter.hasNext(); inter.next()) {
    //print inter.current(); 
}

How can I use EventType defined in template of SFAEventDispatcher class to pass as argument of template for my linked list?

ubi
  • 4,041
  • 3
  • 33
  • 50
wellfer123
  • 69
  • 1
  • 6

2 Answers2

2

EventType is a template parameter. When you write this:

SFALinkedList<SFACallback<EventType> >::iterator iter;

The compiler does not know what is ::iterator: is it a type, or a variable ? By default, it is interpreted as a variable. You have to help it by specifying typename to indicate a type:

typename SFALinkedList<SFACallback<EventType> >::iterator iter;
//^^^^^^                                       ^^^^^^^^^^
//  |                                              |
//  +----------------------------------------------+

For more information, see this SO question.

As usual, gcc is a disaster on the error-reporting side...

Live example here.

Community
  • 1
  • 1
Synxis
  • 9,236
  • 2
  • 42
  • 64
1

EventType is a formal parameter of your template so there's no meaning when you instantiate your template like this (as long as there's no actual class EventType)

SFALinkedList<SFACallback<EventType> >::iterator iter;

You should pass an actual parameter to instantiate the template e.g.,

SFALinkedList<SFACallback<SFAEvent> >::iterator iter;

Also, I believe if you have an actual class EventType compiler wouldn't allow to use it as a formal parameter.

ubi
  • 4,041
  • 3
  • 33
  • 50
  • 1
    @wellfer123 No, If you have a class named `EventType`, you can still have template parameters named `EventType`. Names are 'scoped'. – Synxis Aug 03 '13 at 07:55
  • Also, I believe if you have an actual class EventType compiler wouldn't allow to use it as a formal parameter. (I will try if it work) Thanks – wellfer123 Aug 03 '13 at 07:55