把03列向量类改成了模板类,如此一来,列向量的元素类型不再局限于double,而可以是vector,tensor等。对于模板类而言,要想实现头文件和cpp文件的分开编译处理,头文件中需要对模板类进行实例化的声明。
写好的代码如下:
ColVector.h头文件
// 2020-07-30
// class ColVector -- head file
// A column vector template class
// type could be double, vector, tensor, etc
// Author: Guanhua Mei
#ifndef ColVector_H
#define ColVector_H
#include <iostream>
#include "Vector.h"
template<class T>
class ColVector
{
private:
unsigned int m_nSize; // capicity of column vector, number of elems
T* m_pColVct; // column vector array pointer
public:
// constructors
// construct with size and value
ColVector(unsigned int = 0, T = T());
// copy constructor, deep copy
ColVector(const ColVector<T>&);
// copy assignment, deep copy
ColVector& operator=(const ColVector<T>&);
// move constructor, shallow copy
ColVector(ColVector<T>&&) noexcept;
// move assignment, shallow copy
ColVector& operator=(ColVector<T>&&) noexcept;
// destructor
~ColVector();
// return size
unsigned int size() const {
return m_nSize; }
// get value at idx
T getCompIdx(unsigned int) const;
// set value at idx
void setCompIdx(unsigned int, const T& = T());
// set all
void setAll(const T& = T());
// operator [] to fetch value
T& operator[](unsigned int);
const T& operator[](unsigned int) const;
// operator += -= *= /=, legality wil not be checked!!
ColVector& operator+=(const ColVector&);
ColVector& operator+=(const T&);
ColVector& operator-=(const ColVector&);
ColVector& operator-=(const T&);
ColVector& operator*=(const ColVector&);
ColVector& operator*=(const T&);
ColVector& operator/=(const ColVector&);
ColVector& operator/=(const T&);
// friend operator + - * /, legality wil not be checked!!
template<class Type>
friend ColVector<Type> operator+(const ColVector<Type>&, const ColVector<Type>&);
template<class Type>
friend ColVector<Type> operator+(const ColVector<Type>&, const Type&);
template<class Type>
friend ColVector<Type> operator+(const Type&, const ColVector<Type>&);
template<class Type>
friend ColVector<Type> operator-(const ColVector<Type>&, const ColVector<Type>&);
template<class Type>
friend ColVector<Type> operator-(const ColVector<Type>&, const Type&);
template<class Type>
friend ColVector<Type> operator-(const Type&, const ColVector<Type>&);
template<class Type>
friend ColVector<Type> operator*(const ColVector<Type>&, const ColVector<Type>&);
template<class Type>
friend ColVector<Type> operator*(const ColVector<Type>&, const Type&);
template<class Type>
friend ColVector<Type> operator*(const Type&, const ColVector<Type>&);
template<class Type>
friend ColVector<Type> operator/(const ColVector<Type>&, const ColVector<Type>&);
template<class Type>
friend ColVector<Type> operator/(const ColVector<Type>&, const Type&);
template<class Type>
friend ColVector<Type> operator/(const Type&, const ColVector<Type>&);
// print
void print();
// outputstream operator <<
template<class Type>
friend std::ostream& operator<<(std::ostream&, const ColVector<Type>&);
};
// instantiation template class
template class ColVector<double>;
template class ColVector<Vector>;
// instantiation template friend functions
// instantiation of operator <<
template std::ostream& operator<<(std::ostream&, const ColVector<double>&);
template std::ostream& operator<<(std::ostream&, const ColVector<Vector>&);
// friend functions need to be declarated outside again!
// friend function of outputstream operator <<
template<class T>
std::ostream& operator<<(std::ostream&, const ColVector<T>&);
// friend functions of operator + - * /, legality wil not be checked!!
template<class Type>
ColVector<Type> operator+(const ColVector<Type>&, const ColVector<Type>&);
template<class Type>
ColVector<Type> operator+(const ColVector<Type>&, const Type&);
template<class Type>
ColVector<Type> operator+(const Type&, const ColVector<Type>&);
template<class Type>
ColVector<Type> operator-(const ColVector<Type>&, const ColVector<Type>&);
template<