0

I am working on someone elses code that is no longer around and it is old CodeWarrior Code. XCode complains about this:

template <class listClass,class itemClass>
void FxStreamingObjectList<listClass,itemClass>::StreamOut(FxStream *stream)
{
    if (listClass::size())
    {
        stream->PutSeparator();
        stream->PutString(mFieldName.c_str());
        stream->PutSeparator();
        stream->PutBeginList();
        stream->Indent(+1);

        listClass::iterator iter;

        for (iter=listClass::begin(); iter != listClass::end(); iter++)
        {
            stream->PutSeparator();
            stream->PutString( (*iter)->GetClassID() );
        }

            (*iter)->StreamOut(stream);
        }
        stream->Indent(-1);
        stream->PutSeparator();
        stream->PutEndList();
        stream->PutSeparator();
}

}

I get errors on listClass::iterator iter; and for (iter=listClass::begin(); iter != listClass::end(); iter++) that are:

error: expected `;' before 'iter'
error: 'iter' was not declared in this scope

Other places in the same .h, same types of template declarations I get errors like:

error: dependent-name 'listClass::iterator' is parsed as a non-type, but instantiation yields a type

at:

for (listClass::iterator iter=listClass::begin(); iter != listClass::end(); iter++)

How do I go about solving these errors? I dont know templates all that well so I am confused and not sure where to begin. A good template resource is also appreciated.

一二三
  • 21,059
  • 11
  • 65
  • 74
JTO
  • 121
  • 1
  • 1
  • 3

3 Answers3

5

The compiler doesn't know until a bit later in the parsing process that the specific listClass for any particular instantiation of FxStreamingObjectList<listClass, itemClass> has a member type called iterator. The name iterator is therefore a "dependent name".

You must hint to the compiler that you expect/require iterator here to be a type, with the typename keyword:

typename listClass::iterator iter;

and:

for (typename listClass::iterator it = listClass::begin(), end = listClass::end(); it != end; ++it)

(BTW, are begin() and end() really static member functions?)

Just another C++ quirk. :)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Just 40 seconds ahead of me! Bad Bad. +1 – Nawaz May 03 '11 at 17:06
  • @Tomalak: I think yours is better, with the explanation you provided. So I deleted mine, as it isn't needed anymore. – Nawaz May 03 '11 at 17:10
  • @Tomalak, so anyplace listClass is used I need to prefix with `typename` so the compiler gets the the face it is a type? Still slightly confused with how the chain of events occurs. – JTO May 03 '11 at 17:22
  • @Tomalak - Do I need to change my `for` loop above for more than adding `typename` I get a new error: `error: invalid type in declaration before '=' token` when I do `for (typename iter=listClass::begin(); iter != listClass::end(); iter++)` – JTO May 03 '11 at 17:32
  • @Tomalak - that is how the code is with `begin()` and `end()` I thought it was just `begin` and `end` so I need to find how they are using this. maybe something custom but they picked a bad naming convention – JTO May 03 '11 at 17:47
  • @JTO: You forgot `listClass::iterator`. – Lightness Races in Orbit May 03 '11 at 21:39
  • @JTO: You only need `typename` before a "dependent name". Follow the link in my answer for a detailed discussion on when and where you need this. – Lightness Races in Orbit May 03 '11 at 21:39
1

Officially C++ does not know whether dependent symbols are types or otherwise. To you the programmer listClass::iterator is obviously a type. The compiler needs some help.

The definition of iter should be:

typename listClass::iterator iter
quamrana
  • 37,849
  • 12
  • 53
  • 71
1

The other answers already answered Why the Error
As for the second part of your the Question: A good template resource is also appreciated

The most definitive book on C++ Templates is:
C++ Templates: The Complete Guide by David Vandevoorde & Nicolai Josuttis

Alok Save
  • 202,538
  • 53
  • 430
  • 533