Hello!
I tried searching for this error, but it seems a little uncommon, so I decided to register here.
My problem is, that i would like to declare an iterator of type std::list<std::vector<T> >::iterator within a for statement. I tried it like this:

template <class T>
class CSplit
{
private:
std::list<std::vector<T> > m_Events;
public:
bool Event (int Channel, int Message);
};

template <class T>
bool CSplit<T>::Event (int Channel, int Message)
{
...
for (std::list<std::vector<T> >::iterator i = m_Events.begin (); i != m_Events.end (); i++)
{ ... }
...
return true;
}

The error message returned is:

D:\Programming\C++\Synthie\alpha 0.04\src\Objects\Splitters\CSplitMidi.cpp|14|error: expected ;' before "i"|

It feels as if i had missed out one of these <{([::])}>.
Also, it seems to work fine, if I declare the iterator before the for loop, which is, what I would like to avoid.
I hope, someone has a solution or a clever idea.

Greetings,
JaR

PS: I just realized, that I did not try to declare an iterator but a list, when I tried to get it to work outside the for loop. It does not work, no matter, where I write it. It works, if i change the T to a known type. Any Ideas?

2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by JaR

Nothing criminal with VC++ 2008.
Are you sure that the snippet corresponds to the real context?
What a compiler are you using?

Some tip-comforter - use typedefs in templates:

template <class T>
class CSplit
{
typedef std::list<std::vector<T> > ListVector;
private:
ListVector m_Events;
public:
bool Event (int Channel, int Message);
};

template <class T>
bool CSplit<T>::Event(int Channel, int Message)
{
//...
for (ListVector::iterator i = m_Events.begin();
//...

Aha! MinGW compiler reports error message...

Try this: add typename keyword before i var declaration in for loop

template <class T>
class CSplit
{
typedef std::list<std::vector<T> > ListVector;
private:
ListVector/*std::list<std::vector<T> >*/ m_Events;
public:
bool Event (int Channel, int Message);
};

template <class T>
bool CSplit<T>::Event(int Channel, int Message)
{
//...
for (typename ListVector::iterator i = m_Events.begin();
i != m_Events.end();
i++)
{
//...
}`

That's OK for MinGW (and for VC++ 9 too).
See C++ typename in template definition for more details...

Damn.
I just found out, what the problem was. The compiler does not know, that there is an iterator type within std::vector<T> for every possible T. iterator was interpreted as function or variable. typename in front of the statement fixed the problem.
The really stupid thing: I tried typename earlier, but I put it in the wrong place...
Anyway, thanks for the help though.

Greetings,
JaR

PS: Did not see the new reply. Yes, absolutely correct, thak you.