相关疑难解决方法(0)

我必须在何处以及为何要使用"模板"和"typename"关键字?

在模板,在那里,为什么我必须把typenametemplate上依赖的名字呢?究竟什么是依赖名称?我有以下代码:

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)

c++ templates c++-faq dependent-name typename

1061
推荐指数
8
解决办法
15万
查看次数

模板:我需要更好地学习这些?为什么我会收到错误

我正在研究不再存在的其他代码,它是旧的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 …

c++ templates

0
推荐指数
1
解决办法
96
查看次数

标签 统计

c++ ×2

templates ×2

c++-faq ×1

dependent-name ×1

typename ×1