C++ 指定一些模板参数
我想写一个自己的向量类和一个计算叉积的函数 我的向量类有C++ 指定一些模板参数,c++,templates,vector,C++,Templates,Vector,我想写一个自己的向量类和一个计算叉积的函数 我的向量类有 template<class T, int dim, bool isRowVector> 模板 T是条目的类型,dim是维度,isRowVector指定向量是行向量还是列向量 我重载了向量的一些操作符,尤其是操作符中的 template <class T, int dim, bool isRowVec> std::ostream& operator<<(std::ostream& o
template<class T, int dim, bool isRowVector>
模板
T是条目的类型,dim是维度,isRowVector指定向量是行向量还是列向量
我重载了向量的一些操作符,尤其是操作符中的
template <class T, int dim, bool isRowVec>
std::ostream& operator<<(std::ostream& os, Vector<T, dim, isRowVec>& vec) // <<
{
os << "[" << vec[0];
for(typename std::vector<T>::iterator it = vec.begin()+1; it != vec.end(); it++) {
os << "," << *it;
}
os << "]";
if(!isRowVec) { os << "^T"; }
return os;
}
template <class T, bool isRowVec>
Vector<T, 3, isRowVec> crossProduct (Vector<T, 3, isRowVec> a, Vector<T, 3, isRowVec> b) {
T arr[3] = { a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0] };
Vector<T, 3, isRowVec> newVec(arr);
return newVec;
}
Vector<float,3,true> a = crossProduct(f,g);
Vector<float,3,true>
std::cout << crossProduct(f, g) << std::endl;
error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Vector<float, 3, true>')
std::cout << crossProduct(f, g) << std::endl;
^
std::cout << crossProduct(f, g) << std::endl;
|
|
+------Creates a temporary object (rvalue)
template <class T, int dim, bool isRowVec>
std::ostream& operator<<(std::ostream& os, const Vector<T, dim, isRowVec>& vec)
// ~~~~~
{
// ....
}