相关疑难解决方法(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万
查看次数

C++模板typename迭代器

请考虑以下头文件:

template <typename T> struct tNode
{
    T Data;                      //the data contained within this node
    list<tNode<T>*> SubNodes;       //a list of tNodes pointers under this tNode

    tNode(const T& theData)
    //PRE:  theData is initialized
    //POST: this->data == theData and this->SubNodes have an initial capacity
    //      equal to INIT_CAPACITY, it is set to the head of SubNodes
    {
        this->Data = theData;
        SubNodes(INIT_CAPACITY);   //INIT_CAPACITY is 10
    }

};
Run Code Online (Sandbox Code Playgroud)

现在考虑来自另一个文件的一行代码:

list<tNode<T>*>::iterator it();  //iterate through the SubNodes
Run Code Online (Sandbox Code Playgroud)

编译器给我这个错误消息: Tree.h:38:17: error: need ‘typename’ before ‘std::list<tNode<T>*>::iterator’ because ‘std::list<tNode<T>*>’ …

c++ templates iterator typename

39
推荐指数
3
解决办法
6万
查看次数

将Win代码移植到Linux的错误

可能重复:
我必须在何处以及为何要使用"template"和"typename"关键字?
声明一个C++ set迭代器

我正在尝试编译C++的一些代码,这在Windows系统中可以正常运行.

我有很多错误,如下所示:

code:
..
39          set<Node<T>*>::iterator child;
...
Run Code Online (Sandbox Code Playgroud)

g++ 给我错误:

Node.h:39: error: expected ‘;’ before ‘child’
Run Code Online (Sandbox Code Playgroud)

这只是一个例子.你能给我一些如何解决它的提示吗?

c++ linux porting

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

标签 统计

c++ ×3

templates ×2

typename ×2

c++-faq ×1

dependent-name ×1

iterator ×1

linux ×1

porting ×1