我試圖使用朋友函數來重載< <和模板以熟悉模板。我不知道這些編譯錯誤是:朋友,模板,重載<<
Point.cpp:11: error: shadows template parm 'class T'
Point.cpp:12: error: declaration of 'const Point<T>& T'
此文件
#include "Point.h"
template <class T>
Point<T>::Point() : xCoordinate(0), yCoordinate(0)
{}
template <class T>
Point<T>::Point(T xCoordinate, T yCoordinate) : xCoordinate(xCoordinate), yCoordinate(yCoordinate)
{}
template <class T>
std::ostream &operator<<(std::ostream &out, const Point<T> &T)
{
std::cout << "(" << T.xCoordinate << ", " << T.yCoordinate << ")";
return out;
}
我的頭看起來像:
#ifndef POINT_H
#define POINT_H
#include <iostream>
template <class T>
class Point
{
public:
Point();
Point(T xCoordinate, T yCoordinate);
friend std::ostream &operator<<(std::ostream &out, const Point<T> &T);
private:
T xCoordinate;
T yCoordinate;
};
#endif
我的頭也給出了警告:
Point.h:12: warning: friend declaration 'std::ostream& operator<<(std::ostream&, const Point<T>&)' declares a non-template function
我還不確定爲什麼。有什麼想法嗎?謝謝。
你在用什麼編譯器?該代碼甚至適用於VC6 – YeenFei 2010-06-03 07:03:07
@YeenFei:一切工作在VC6! – 2010-06-03 07:05:50
XCode IDE。那是GNU編譯器嗎? – Crystal 2010-06-03 07:14:59