相关疑难解决方法(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++ set迭代器

可能重复:
我必须在何处以及为何要使用"template"和"typename"关键字?
c ++模板typename迭代器

以下代码将无法编译:

#include <iostream>
#include <set>
using namespace std;

template<class T>
void printSet(set<T> s){
    set<T>::iterator it;
}

int main(int argc, char** argv){
    set<int> s;
    printSet<int>(s);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误说:

set.cpp: In function ‘void printSet(std::set<T, std::less<_Key>, std::allocator<_CharT> >)’:
set.cpp:7: error: expected `;' before ‘it’
set.cpp: In function ‘void printSet(std::set<T, std::less<_Key>, std::allocator<_CharT> >) [with T = int]’:
set.cpp:12:   instantiated from here
set.cpp:7: error: dependent-name ‘std::set<T,std::less<_Key>,std::allocator<_CharT> >::iterator’ is parsed as a non-type, but instantiation yields a type
set.cpp:7: note: say …
Run Code Online (Sandbox Code Playgroud)

c++ templates iterator compiler-errors

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