【问题标题】:template specialization -> 'too few template-parameter-lists'模板专业化 -> '模板参数列表太少'
【发布时间】:2012-05-02 10:17:34
【问题描述】:

我找不到这段代码有什么问题:

template <class T>
class B{
    T _t;
public:
    B(T t) : _t(t) {}
    void printHello();
};

template <class T>
void B<T>::printHello(){
    std::cout << "Hello";
}
void B<char*>::printHello(){
    std::cout <<"Good bye!";
}

我不断得到:

'错误:模板参数列表太少'

【问题讨论】:

    标签: c++ templates compiler-errors template-specialization


    【解决方案1】:

    在专业化之前缺少template&lt;&gt;

    template<>
    void B<char*>::printHello(){
        std::cout <<"Good bye!";
    }
    

    【讨论】:

    • #include <iostream> 模板 <class t> class B{ T _t;公共:B(T t):_t(t){} void printHello(); };模板 <class t> void B<t>::printHello(){ std::cout ::printHello(){ std::cout b(5); b.printHello(); char* str = "iko"; B<char> c(str); c.printHello(); } </char></t></class></class></iostream>
    • 添加了template&lt;&gt;,我没有收到任何此类错误。您使用的是哪个版本的 g++?
    • 哦...对不起...我的错...我重新启动 Eclipse,现在它可以工作了...谢谢!
    猜你喜欢
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2020-12-27
    相关资源
    最近更新 更多