试图让操作员工作,但给我带来了一堆错误:
我的头文件
template <unsigned short n>
class Vector {
public:
std::vector<float> coords;
Vector();
Vector(std::vector<float> crds);
friend std::ostream& operator <<(std::ostream& out, const Vector& v);
};
template <unsigned short n>
Vector<n>::Vector() {
coords.assign(n, 0.0);
}
template <unsigned short n>
std::ostream& operator<<(std::ostream& out, const Vector<n>& v) {
out << "(" << v.coords[1] << " - " << v.coords[2] << ")";
return out;
}
测试文件
#include <iostream>
#include "vector.h"
using namespace std;
int main() {
Vector<3> toomas;
cout << toomas;
}
错误:
C:\CodeBlocks\kool\praks3\vector.h|14|警告:友元声明 'std::ostream& operator<<(std::ostream&, const Vector&)' 声明一个非模板函数|
C:\CodeBlocks\kool\praks3\vector.h|14|注意:(如果这不是您想要的,请确保函数模板已经声明并在此处的函数名称后添加<>)|
obj\Debug\test.o||在函数'main'中:|
C:\CodeBlocks\kool\praks3\test.cpp|8|未定义引用 `operator<<(std::ostream&, Vector<(unsigned short)3> const&)'|