List iterator of a structure containing a template type.

I have a little c++ experience, but I still struggle with some of the syntax sometimes. I created a template class and am having some problems. The following code compiles in Visual Studio 2005 without a warning, but in Linux (and QT) it gives this error:
hist.h:14: error: expected `;' before 'listitr'

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <list>

template <class HistClassType>
class DataHistogram
{
   typedef struct
   {
      HistClassType value;
      int count;
   } tHistValueCountPair;
public:
   void blah(void)
   {
      std::list<tHistValueCountPair>::iterator listitr;
   }
};

int main(void)
{
   DataHistogram<int> hist;
}


The line in error is this:
std::list<tHistValueCountPair>::iterator listitr;
if I replace it with this:
std::list<tHistValueCountPair> listitr;
it compiles in Linux and Visual Studio 2005.

So it looks like the Linux compiler can handle a list of the template structure type, but not an iterator for the template structure type.

So my question is, what am I doing wrong that my Linux compiler cannot handle? I want this piece of code to be completely portable across all c++ compilers.

Thanks,

Dan



the two line are not the same. The second declares a variable of type std::list<tHistValueCountPair> called listitr, the first declares an iterator to a std::list<tHistValueCountPair> type.

try renaming your struct

1
2
3
4
typedef struct tHistValueCountPair
{
...
};


btw you don't really need to use typedef here.
1
2
// dependent name needs disambiguation
typename std::list<tHistValueCountPair>::iterator listitr ;

http://womble.decadent.org.uk/c++/template-faq.html#disambiguation
because its referring to a type.

Thanks for that.
As per JLBorges suggestion, I added 'typename' before the line in error. Everything now works in both VS and linux.

Thanks for all the help!!
Last edited on
Topic archived. No new replies allowed.