为什么编译不了??
还有就是
这个定义的解释函数内就不用了解释了
这是教材
你24行漏了个<T>
应该是Vector2D<T> Vector2D<T>::add(...)
template <typename T>
class Vector2D
{
public:
Vector2D(T X,T Y) : x(X),y(Y) {}
template<typename RType>
Vector2D add(const RType &rhs);
void print() {cout<<"("<<x<<","<<y<<")"<<endl;}
private:
T x,y;
};
template <typename T>
template <typename RType>
Vector2D<T> Vector2D<T>::add(const RType &rhs)
{
x += rhs;
y += rhs;
return *this;
}
int main()
{
Vector2D<int> v1(1,2);
int intNum = 3;
float floatNum = 1.9;
cout<<"v1加上intNum的结果为:"<<endl;
(v1.add<int>(intNum)).print();
cout<<"v1加上floatNum的结果为:"<<endl;
(v1.add<float>(floatNum)).print();
return 0;
}