所以我有我的模板類的一些問題。C++類嵌套到模板類
<!-- language: lang-c++ -->
template<class T>
class List {
class Counter
{
T real;
T imaginary;
Counter *next;
//....
public:
Counter(T a=0,T b=0);
virtual ~Counter();
friend ostream& operator<<(ostream&,Counter&);
friend ostream& operator<<(ostream&,List&);
};
Counter* first;
Counter* last;
//....
};
但我有一些方法的問題。如果我寫的功能
template<class T> Counter operator/(Counter &one,...)
當我在VC++ 10看櫃檯它說,像
<error_type>&one
例如。我應該在代碼中的任何地方使用template<class T>
作爲我的Counter類嗎?
//Methods
//Counter. For example some methods
Counter operator/(Counter& one,Counter& two){}
ostream& operator<<(ostream&os,Counter&x){}
istream& operator>>(istream&is,Counter&x){}
//List
template<class T>void List<T>::add(Counter *T,int i,bool c){}
是的,你應該。除非您定義函數內聯,否則您需要爲每個方法定義提供模板參數。 – atoMerz
據我所知,是的,如果你需要在計數器中使用模板類。 – swtdrgn