Template Class Member Confusion

Hello Folks:

I don't use templates very often.

I'm doing something stupid.

Visual Studio doesn't like my list's const_iterator type declaration.

The following code causes Visual Studio to generate these warnings and errors:

circular_buffer.h(14,1): warning C4346: 'const_iterator': dependent name is not a type
circular_buffer.h(14,1): message : prefix with 'typename' to indicate a type
circular_buffer.h(16): message : see reference to class template instantiation 'CIRCULAR_BUFFER<T>' being compiled
circular_buffer.h(14,1): error C2061: syntax error: identifier 'const_iterator'
circular_buffer.h(14,1): error C2238: unexpected token(s) preceding ';'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H

#include <iostream>
#include <list>

template <class T>
class CIRCULAR_BUFFER
{
    typedef std::list <T*> CIRCULAR_BUFFER_LIST;

private:

    CIRCULAR_BUFFER_LIST::const_iterator test_iterator;
    CIRCULAR_BUFFER_LIST buffer;
};
#endif 


The code compiles if I comment the template stuff out and make the list a list of type int.

The compilation fails even if I never actually declare a variable of type CIRCULAR_BUFFER.

What am I doing wrong?

Thanks
Larry
Last edited on
Do what the message suggest:

typename CIRCULAR_BUFFER_LIST::const_iterator test_iterator;
Thanks coder777:

I don't remember seeing the typename key word before.

I've taken a look at several pages on the topic, but I'm not getting it yet.

I'm still not sure why CIRCULAR_BUFFER_LIST::const_iterator isn't recognized to be a type name. CIRCULAR_BUFFER_LIST is an std::list so declaring a variable to be a const_iterator of an std::list doesn't seem to me to be particularly confusing.

I appreciate you setting me on the trail to figure that out.

Larry
Last edited on
I'm still not sure why CIRCULAR_BUFFER_LIST::const_iterator isn't recognized to be a type name.
That's a requirement by the language. The reason is probably that the compiler needs to know whether the expression is supposed to be resolved as type or a function.
Thanks again coder777:

I need to get up to find some time to learn new versions of C++.
Topic archived. No new replies allowed.