[英]c++: C2783 error instanting class template
Excuse me in advance for my bad english..i'm developing my own STL and i have some problem using template. 请提前为我的英语不好打扰。.我正在开发自己的STL,使用模板时遇到一些问题。 This is the main structure of the my List class: 这是my List类的主要结构:
List.h
template <class T, class N>
class List{
public:
//public methods
private:
//private methods
}
Ok, now i need to implement a member function "sort" that will sort the elements of the list, but in this sort function i need to istantiate a List object. 好的,现在我需要实现一个成员函数“ sort”,该成员函数将对列表的元素进行排序,但是在这种排序函数中,我需要设置一个List对象。 I found a solution implementing that function in a different module in this way: 我找到了以这种方式在另一个模块中实现该功能的解决方案:
AuxiliaryFunction.h
#include<List.h>
template <class List>
void sort(Lista & l){
List A; //Works fine!
..
}
But in this way i have to call the sort function by doing this: 但是以这种方式,我必须通过执行以下操作来调用sort函数:
sort(ListObject);
instead of 代替
ListObject.sort();
How can I do the same thing but with a member function of the List class? 我如何使用List类的成员函数来做同样的事情? What would be different if the List class was an abstract class like the following code? 如果List类是类似于以下代码的抽象类,会有什么不同?
template <class T, class N>
class List{
public:
typedef T ElemType;
typedef P position;
virtual void create() = 0;
virtual bool empty() = 0;
..
void merge(Lista< T, N > &, Lista< T, N > &);
...
private:
//private methods
}
Can be a template template parameter be a solution? 可以将模板template参数作为解决方案吗? I tried by doing something like this 我尝试做这样的事情
template <class T, class N>
class List{
...
template <template <class T,class N> class List> sort(){
List A;
..
}
}
but a C2783 error ('could not deduce template argument for List') appear when i write 但是在我编写时出现了C2783错误(“无法推断出列表的模板参数”)
ListObject.sort(); ListObject.sort();
Any kind of help will be appreciated :) 任何帮助将不胜感激:)
It sounds like you're looking for something like this: 听起来您正在寻找这样的东西:
template <class T, class N>
class List{
...
void sort(){
List<T, N> A;
..
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.