如何在类声明之外声明模板函数 [英] How do I declare template function outside the class declaration

查看:48
本文介绍了如何在类声明之外声明模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iterator>
#include <map> 
#include <vector>

template <class T1, class T2>
class A
{
public:

    typedef typename std::vector<std::pair<T1,T2> >::iterator iterator;

    std::pair<iterator, bool > foo()
    {
        iterator aIter;
        return std::pair<std::vector<std::pair<T1,T2> >::iterator, bool >(aIter ,false);
    }
};

上面的代码对我来说很好用.但我想将函数的定义移到类声明之外.我试过了.

The above code works fine for me. But I want to move the definition of the function outside the the class declaration. I tried this.

template <class T1, class T2>
class A
{
public:

    typedef typename std::vector<std::pair<T1,T2> >::iterator iterator;

    std::pair<iterator, bool > foo();
};

template <class T1, class T2>
std::pair<std::vector<std::pair<T1,T2> >::iterator, bool > A<T1, T2>::foo()
{
    iterator aIter;
    return std::pair<std::vector<std::pair<T1,T2> >::iterator, bool >(aIter ,false);
}

但它没有编译.知道如何做到这一点吗?

But it is not compiling. Any Idea how to do this?

推荐答案

您再次缺少返回值中的类型名称.函数应该是:

You are again missing the typename in the return value. The function should be:

template <class T1, class T2>
std::pair<typename std::vector<std::pair<T1,T2> >::iterator, bool > A<T1, T2>::foo()
{
    iterator aIter;
    return std::pair<std::vector<std::pair<T1,T2> >::iterator, bool >(aIter ,false);
}

这篇关于如何在类声明之外声明模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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