在模板,在那里,为什么我必须把typename和template上依赖的名字呢?究竟什么是依赖名称?我有以下代码:
template <typename T, typename Tail> // Tail will be a UnionNode too.
struct UnionNode : public Tail {
// ...
template<typename U> struct inUnion {
// Q: where to add typename/template here?
typedef Tail::inUnion<U> dummy;
};
template< > struct inUnion<T> {
};
};
template <typename T> // For the last node Tn.
struct UnionNode<T, void> {
// ...
template<typename U> struct inUnion {
char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any …Run Code Online (Sandbox Code Playgroud) 我正在研究不再存在的其他代码,它是旧的CodeWarrior代码.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();
}
Run Code Online (Sandbox Code Playgroud)
}
我得到错误listClass::iterator iter;,for (iter=listClass::begin(); iter != listClass::end(); iter++)那是:
error: expected `;' before 'iter'
error: 'iter' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
同一.h中的其他地方,相同类型的模板声明我得到的错误如下:
error: dependent-name 'listClass::iterator' is parsed as a non-type, but instantiation yields a type
Run Code Online (Sandbox Code Playgroud)
在:
for (listClass::iterator …