0

I have this function:

template <typename T>
List<T>::ListNode *List<T>::find(int index) const
{
    if ( (index < 1) || (index > getLength()) )
        return NULL;
    else
    {
        ListNode *cur = head;
        for (int skip = 1; skip < index; ++skip)
            cur = cur->next;
        return cur;
    }
}

That is giving me these two errors, each on the second line:

expected constructor, destructor, or type conversion before '*' token
expected `;' before '*' token

All my other methods that use templates work just fine. I think the problem is that my syntax where I am calling my ListNode struct is wrong. I had this working without templates earlier and now I'm trying to implement it with templates and I am getting these errors.

1
  • is getLength() using a type specific method?
    – clamchoda
    Commented Apr 27, 2011 at 3:44

1 Answer 1

8

It should be

template <typename T>
typename List<T>::ListNode *List<T>::find(int index) const
// ...

typename tells the compiler that List<T>::ListNode represents a type. When inside a template, there is a parsing ambiguity when :: is encountered. You therefore have to use the typename keyword when whatever follows :: is a type.

1
  • @Max: I was going to but you have to wait 15 or so minutes to do so. Commented Apr 27, 2011 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.