[英]Templates: I need to learn these better? Why am i getting errors
I am working on someone elses code that is no longer around and it is old CodeWarrior Code.我正在研究不再存在的其他人的代码,它是旧的 CodeWarrior 代码。 XCode complains about this:
XCode 抱怨这个:
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;
我在
listClass::iterator iter;
and for (iter=listClass::begin(); iter:= listClass:;end(); iter++)
that are: for (iter=listClass::begin(); iter:= listClass:;end(); iter++)
是:
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:在 same.h 中的其他地方,相同类型的模板声明我得到如下错误:
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.
一个好的模板资源也值得赞赏。
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
.编译器直到解析过程稍后才知道
FxStreamingObjectList<listClass, itemClass>
的任何特定实例化的特定listClass
具有称为iterator
的成员类型。 The name iterator
is therefore a "dependent name".因此,名称
iterator
是一个“依赖名称”。
You must hint to the compiler that you expect/require iterator
here to be a type, with the typename
keyword:您必须使用
typename
关键字向编译器提示您期望/需要iterator
是一种类型:
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?) (顺便说一句,
begin()
和end()
真的是static 成员函数吗?)
Just another C++ quirk.只是另一个 C++ 怪癖。 :)
:)
Officially C++ does not know whether dependent symbols are types or otherwise.官方 C++ 不知道依赖符号是类型还是其他。 To you the programmer
listClass::iterator
is obviously a type.对你来说,程序员
listClass::iterator
显然是一种类型。 The compiler needs some help.编译器需要一些帮助。
The definition of iter
should be: iter
的定义应该是:
typename listClass::iterator iter
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++ 模板的最权威的书是:
C++ Templates: The Complete Guide by David Vandevoorde & Nicolai Josuttis C++ 模板: David Vandevoorde 和 Nicolai Josuttis的完整指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.