我如何在类声明之外声明模板函数 [英] How do I declare template function outside the class declaration
本文介绍了我如何在类声明之外声明模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#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屋!
查看全文