我一直在尝试创建一个类笛卡尔,其中对象是笛卡尔点(int 或 double)上的 2 个点。然后我想重载<<。我收到错误消息:
Undefined symbols for architecture x86_64:
"cartesian<double, int>::cartesian(double, int)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我不明白我的错误在哪里。
标题
#include <iostream>
using namespace std;
template <class first, class second>
class cartesian
{
public:
cartesian(first, second);
//double getslope(cartesian &obj1, cartesian &obj2);
friend ostream& operator<< (ostream &out, cartesian &cPoint);
private:
first x;
second y;
};
CPP 文件
#include "cartesian.h"
#include <iostream>
using namespace std;
template<class first,class second>
cartesian<first, second>::cartesian(first a, second b)
:x(a), y(b)
{}
/*
// between obj1 and obj2
template<class first,class second>
double cartesian<first, second>::getslope(cartesian &obj1, cartesian &obj2){
return ((obj2.y-obj1.y)/(obj2.x-obj1.y));
}
*/
template<class first,class second>
ostream& operator<< (ostream &out,const cartesian<first, second> &cPoint)
{
// Since operator<< is a friend of the Point class, we can access
// Point's members directly.
out << "(" << cPoint.x << ", " <<
cPoint.y << ")";
return out;
}
主要的
#include <iostream>
#include "cartesian.h"
using namespace std;
int main()
{
cartesian<double, int> ob11(3.4, 6);
return 0;
}