显式模板专业化错误 [英] Explicit template specialization error

查看:47
本文介绍了显式模板专业化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很容易.我在玩模板,但是出现编译器错误.

this one should be pretty easy. I'm playing with templates, but get a compiler error.

#include <iostream>

template <class T1, class T2>
class Pair
{
    private:
        T1 a;
        T2 b;
    public:
        T1& first();
        T2& second();
        Pair(const T1& aval, const T2& bval) : a(aval), b(bval) {}
};

template <class T1, class T2>
T1& Pair<T1,T2>::first()
{
    return a;
}


template <class T1, class T2>
T2& Pair<T1,T2>::second()
{
    return b;
}

// Explicit Specialization
template <>
class Pair<double, int>
{
    private:
        double a;
        int b;
    public:
        double& first();
        int& second();
        Pair(const double& aval, const int& bval) : a(aval), b(bval) {}
};

template <>
double& Pair<double,int>::first()
{
    return a;
}

template <>
int& Pair<double,int>::second()
{
    return b;
}


int main(int argc, char const *argv[])
{

    Pair<int, int> pair(5,6);
    //Pair<double,int> pairSpec(43.2, 5);
    return 0;
}

错误看起来像这样

main.cpp:42:27: error: no function template matches function template specialization 'first'
double& Pair<double,int>::first()
                          ^
main.cpp:49:24: error: no function template matches function template specialization 'second'
int& Pair<double,int>::second()

关于可能出什么问题的任何线索吗?

Any clue to what could be going wrong?

推荐答案

在方法声明之前不需要模板<>声明.

You don't need template<> declaration before method declaration.

double& Pair<double,int>::first() {
    return a;
}
int& Pair<double,int>::second() {
   return b;
}

应该足够.

这篇关于显式模板专业化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆