我有Point
类,它有X
,Y
和Name
作为数据成员。我重载C++中的“未定义符号”错误
T operator-(const Point<T> &);
这种计算两个点之间的距离,并返回一个值
template < typename T>
T Point<T>::operator-(const Point<T> &rhs)
{
cout << "\nThe distance between " << getName() << " and "
<< rhs.getName() << " = ";
return sqrt(pow(rhs.getX() - getX(), 2) + pow(rhs.getY() - getY(), 2));;
}
的main
功能
int main() {
Point<double> P1(3.0, 4.1, "Point 1");
Point<double> P2(6.4, 2.9, "Point 2");
cout << P2 - P1;
return EXIT_SUCCESS;
}
但问题是,这个程序不编译,我收到此错误:
Undefined symbols:
"Point<double>::operator-(Point<double>&)", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
任何帮助表示赞赏...
你有没有包括运营商的标头中的实现,或在.cpp文件? – juanchopanza 2012-07-24 10:25:51
@juanchopanza是的。我只有一个.cpp文件,它具有实现。 – 2012-07-24 10:31:08
看到我的回答下面 – 2012-07-24 10:34:07