2012-12-20 50 views
6

我用gcc/4.7,我需要实例化的模板函数(或成员函数)的模板,模板参数的类。我收到以下错误模板模板代码不工作

test.cpp: In function 'void setup(Pattern_Type&)': 
test.cpp:17:34: error: type/value mismatch at argument 1 in template parameter list for 'template<template<class> class C> struct A' 
test.cpp:17:34: error: expected a class template, got 'typename Pattern_Type::traits' 
test.cpp:17:37: error: invalid type in declaration before ';' token 
test.cpp:18:5: error: request for member 'b' in 'a', which is of non-class type 'int' 

通过注释标注在片段中的两行代码运行,所以一个能在“主”,但不是“设置”实例化。我想,这将是为其他人的利益,我会很高兴地理解为什么代码不起作用的原因。这里的代码

struct PT { 
    template <typename T> 
    struct traits { 
    int c; 
    }; 
}; 

template <template <typename> class C> 
struct A { 
    typedef C<int> type; 
    type b; 
}; 

template <typename Pattern_Type> 
void setup(Pattern_Type &halo_exchange) { 
    A<typename Pattern_Type::traits> a; // Line 17: Comment this 
    a.b.c=10; // Comment this 
} 

int main() { 
    A<PT::traits> a; 

    a.b.c=10; 

    return 0; 
} 

感谢您的任何建议和修复! 莫罗

+0

是否MSVC10下编译。 –

回答

9

您需要标记Pattern_Type::traits为模板:

A<Pattern_Type::template traits> a; 

这是必要的,因为它是依赖于模板参数Pattern_Type

你也不应使用typename那里,因为traits是一个模板,而不是一个类型。

+0

这是一个组合,我没有尝试:模板而不类型名称。不过我不清楚为什么在“主”我并不需要指定“模板”。感谢超级快速回答! – user1919074

+1

@ user1919074:这是因为在'main'中它不依赖于模板参数(即PT的内容已经知道,但Pattern_Type的内容未知,因为它可能是任何东西)。有关更多信息,请参阅[我在哪里以及为什么必须放置“模板”和“类型名称”关键字?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have- to-put-the-template-and-typename-keywords) – interjay

+0

没错,我应该知道这个... – user1919074