我有一个名为“ Point”的模板类,并且使用了<<操作符的重载来写入ostream
。我尝试做这样的事情:
Point<int> point = Point(4, 3);
stream << point << endl;
当我编译程序时,出现链接器错误,提示未找到带有参数(ostream&,const Point&)的运算符<<的重载。
//point.h -- headers definitions for Point<>
#pragma once
#include <iostream>
#include "serializable.h"
//Class that represents coordinates of point on the plane
template <class T>
class Point
{
public:
T x;
T y;
Point();
Point(T _x, T _y);
bool operator==(Point & p)
{
return p.x == x && p.y == y;
}
Point<T> & operator=(const Point<T> &p)
{
x = p.x;
y = p.y;
return *this;
}
friend std::ostream & operator<<(std::ostream & stream, const Point<T> & point);
friend std::istream & operator>>(std::istream & stream, Point<T> & point);
};
template <class T>
Point<T>::Point()
{
}
template <class T>
Point<T>::Point(T _x, T _y)
{
x = _x;
y = _y;
}
template <class T>
std::ostream & operator<<(std::ostream & stream, const Point<T> & point)
{
stream << point.x << " " << point.y << std::endl;
return stream;
}
template <class T>
std::istream & operator>>(std::istream & stream, Point<T> & point)
{
stream >> point.x >> point.y;
return stream;
}
这是错误文本:
Error 209 error LNK2019: reference to unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Point<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Point@H@@@Z) in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Monster const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVMonster@@@Z) monster.obj The Pacman