好吧我有點卡住試圖超載模板類的< <運算符。要求是運算符必須調用爲這個類定義的void打印函數。operator << overloading調用打印函數的麻煩
下面是從模板頭重要的東西:
template <class T>
class MyTemp {
public:
MyTemp(); //constructor
friend std::ostream& operator<< (std::ostream& os, const MyTemp<T>& a);
void print(std::ostream& os, char ofc = ' ') const;
,這裏是我的打印功能基本上它是一個載體,並打印最後一個元素到第一個:
template <class T>
void Stack<T>::print(std::ostream& os, char ofc = ' ') const
{
for (int i = (fixstack.size()-1); i >= 0 ; --i)
{
os << fixstack[i] << ofc;
}
}
,這裏是我如何有運營商< <過載:
template <class T>
std::ostream& operator<< (std::ostream& os, const Stack<T>& a)
{
// So here I need to call the a.print() function
}
但我收到「無法解析的外部符號」錯誤。所以真的我想我有兩個問題。第一,是解決上述錯誤的方法。其次,一旦這個問題得到解決,我會在< <過載內調用a.print(os)?我知道它需要返回一個ostream。任何幫助將不勝感激!
閱讀此常見問題解答:http://www.parashift.com/c++-faq/templates.html#faq-35.16 – 2012-02-23 07:20:43
您在第一個片段中調用類模板「MyTemp」,在其他片段中調用「Stack」。你是否在你的真實代碼中這樣做? – 2012-02-23 07:21:57