求改错,C++语言用template实现vector运行功能 环境VS2010
我真穷,刚申请的号,没有分啊,求大神解救。
/vector.h/
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
using namespace std;
template<class Type>
classVector {
private :
Type * vPtr;
Type vSize;
public :
Vector ( int ) ; // specify number of elements, no initial values
Vector ( int, const Type ) ; // specify elements, initial constant
Vector ( int, Type * ) ; // specify elements, assign array
Vector ( const Vector<Type>& ) ;
virtual ~Vector () ;
Vector<Type>& operator= ( const Vector<Type>& ) ; // vector = vector
Vector<Type>& operator= ( const Type * ); // vector = array
friend ostream& operator<< ( ostream&, const Vector<Type>& ) ;
friend istream& operator>> ( istream&, const Vector<Type>&);
friend int operator== ( const Vector&, const Vector& ) ;
friend int operator!= ( const Vector&, const Vector& ) ;
friend Vector<Type> operator+ ( const Vector<Type>&, const Vector<Type>& ) ;
friend Vector<Type> operator- ( const Vector<Type>&, const Vector<Type>& ) ;
friend Vector<Type> operator- ( const Vector<Type>& ) ; // negate
friend Vector<Type> operator* ( const Type&, const Vector<Type>& ) ;
friend Vector<Type> operator* ( const Vector<Type>&, const Type& ) ;
friend Vector<Type> operator/ ( const Vector<Type>&, const Type& ) ;
friend Vector<Type> operator/ ( const Type&, const Vector<Type>& ) ;
Vector<Type>& operator+= ( const Vector<Type>& ) ;
Vector<Type>& operator-= ( const Vector<Type>& ) ;
Vector<Type>& operator+= ( const Type& ) ;
Vector<Type>& operator-= ( const Type& ) ;
Vector<Type>& operator*= ( const Type& ) ;
Vector<Type>& operator/= ( const Type& ) ;
Vector<Type>& operator-- ( ) ; // predecrement
Vector<Type> operator-- ( int ) ; // postdecrement
Vector<Type>& operator++ ( ) ; // preincrement
Vector<Type> operator++ ( int ) ; // postincrement
// the "index" operator
int &operator()(int)const;
// Member Functions
// Dot Product
friend double dot ( const Vector<Type>&, const Vector<Type>& ) ;
// Normalization, Make it a unit Vector
void normalize () ;
// Vector Length
double length () const ;
} ;
#endif
/vector.cpp/
#include "Vector.h"
#include<istream>
#include<ostream>
#include "string"
#include <stdlib.h>
using namespace std;
template<class Type>
Vector<Type>::Vector(int vecSize)
{
if(vecSize<0)
vecSize=0;
this->vSize=vecSize;
vPtr=NULL;
if(vSize>0)
vPtr=new Type[vSize];
}
template<class Type>
Vector<Type>::Vector(int vecSize,const Type value)
{
if(vecSize<0)
vecSize=0;
this->vSize=vecSize;
vPtr=NULL;
if(vecSize>0)
vPtr=new Type[vecSize];
for(int i=0;i<vecSize;i++)
vPtr[i]=value;
}
template<class Type>
Vector<Type>::Vector(int vecSize,Type *valuer)
{
if(vecSize<0)
vecSize=0;
this->vSize=vecSize;
vPtr=NULL;
vSize=vecSize;
if(vecSize>0)
vPtr=new Type[vSize];
for(int i=0;i<vSize;i++)
{
vPtr[i]=valuer[i];
}
}
//cpoy constructor
template<class Type>
Vector<Type>::Vector(const Vector<Type>&rhs)
{
vSize=rhs.vSize;
vPtr=new Type[vSize];
for(int i=0;i<vSize;i++)
{
vPtr[i]=rhs.vPtr[i];
}
}
template<class Type>
Vector<Type>::~Vector()
{
delete []vPtr;
}
//operator=
template<class Type>
Vector<Type>& Vector<Type>::operator=(const Vector<Type> &rhs)
{
if(&rhs !=this)
{
if(vSize>0)
delete[]this->vPtr;
vPtr=new Type[rhs.vSize];
for(int i=0;i<rhs.vSize;i++)
vPtr[i]=rhs.vPtr[i];
this->vSize=rhs.vSize;
}
return *this;
}
template<class Type>
Vector<Type>& Vector<Type>::operator= ( const Type *input)
{
if(vSize>0)
vPtr=new Type[vSize];
for(int i=0;i<vSize;i++)
{
this->vPtr[i]=input[i];
}
return *this;
}
template<class Type>
ostream& operator<<(ostream &os,const Vector<Type>& rhs)
{
if(rhs.length()==0)cout<<"It is an empty Vector"<<endl;
os<<"The length of vector is"<<rhs.length()<<endl;
for(int i=0;i<rhs.vSize;i++)
os<<"Vector["<<i<<"]"<<rhs(i)<<endl;
return os;
}
template<class Type>
istream& operator>>(istream &is,const Vector<Type>& rhs)
{
//is>>rhs.vSize;
for(int i = 0;i<rhs.vSize;i++)
{
is>>rhs.vPtr[i];
}
return is;
}
//operator==
template<class Type>
int operator== ( const Vector<Type>& chs, const Vector<Type>& rhs)
{
int flag=0;
if(chs.length()==rhs.length())
{
for(int i=0;i<chs.length();i++)
{
if(chs(i)!=rhs(i))
return 0;
}
flag=1;
}
return flag;
}
template<class Type>
int operator!= (const Vector<Type>& chs, const Vector<Type>& rhs )
{
return!(chs==rhs);
}
//operator+
template<class Type>
Vector<Type> operator+(const Vector<Type>&chs,const Vector<Type>&rhs)
{
if(chs.vSize!=rhs.vSize)cout<<"They are not match";
return -1;
Vector<Type> temp(chs.vSize);
for(int i=0;i<chs.vSize;i++)
temp.vPtr[i]=chs.vPtr[i]+rhs.vPtr[i];
return temp;
}
//operator-
template<class Type>
Vector<Type> operator-(const Vector<Type>&chs,const Vector<Type>&rhs)
{
if(chs.vSize!=rhs.vSize)cout<<"They are not match";
return -1;
Vector<Type> temp(chs.vSize);
for(int i=0;i<chs.vSize;i++)
temp.vPtr[i]=chs.vPtr[i]-rhs.vPtr[i];
return temp;
}
//operator negative
template<class Type>
Vector<Type> operator-(const Vector<Type>&rhs)
{
Vector<Type> temp(rhs.vSize);
for(int i=0;i<rhs.vSize;i++)
temp.vPtr[i]=rhs.vPtr[-i];
return temp;
}
//operator *
template<class Type>
Vector<Type> operator*(const Type& dommy,const Vector<Type>&rhs)
{
Vector<Type> temp(rhs.vSize);
for(int i=0;i<rhs.vSize;i++)
temp.vPtr[i]=dommy*rhs.vPtr[i];
return temp;
}
template<class Type>
Vector<Type> operator*(const Vector<Type>&chs,const Type& dommy)
{
Vector<Type> temp(chs.vSize);
for(int i=0;i<chs.vSize;i++)
temp.vPtr[i]=dommy*chs.vPtr[i];
return temp;
}
//operator/
template<class Type>
Vector<Type> operator/(const Type& dommy,const Vector<Type>&rhs)
{
Vector<Type> temp(rhs.vSize);
for(int i=0;i<rhs.vSize;i++)
temp.vPtr[i]=dommy/rhs.vPtr[i];
return temp;
}
template<class Type>
Vector<Type> operator/(const Vector<Type>&chs,const Type& dommy)
{
Vector<Type> temp(chs.vSize);
for(int i=0;i<chs.vSize;i++)
temp.vPtr[i]=chs.vPtr[i]/dommy;
return temp;
}
//operator+=
template<class Type>
Vector<Type>& Vector<Type>::operator+=(const Vector<Type>&rhs)
{
if(vSize==rhs.vSize)
{
for(int i=0;i<vSize;i++)
this->vPtr[i]+=rhs.vPtr[i];
}
else
{
delete[]vPtr;//nure=chs
vPtr=new Type[rhs.vSize];
vSize=rhs.vSize;
for(int i=0;i<vSize;i++)
this->vPtr[i]+=rhs.vPtr[i];
}
return *this;
}
//operator-=
template<typename Type>
Vector<Type>& Vector<Type>::operator-=(const Vector<Type>&rhs)
{
if(vSize==rhs.vSize)
{
for(int i=0;i<vSize;i++)
this->vPtr[i]-=rhs.vPtr[i];
}
else
{
delete[]vPtr;
vPtr=new Type[rhs.vSize];
vSize=rhs.vSize;
for(int i=0;i<vSize;i++)
this->vPtr[i]-=rhs.vPtr[i];
}
return *this;
}
//operator+= double
template<class Type>
Vector<Type>& Vector<Type>::operator+=(const Type& dommy)
{
for(int i=0;i<this->vSize;i++)
{
this->vPtr[i]+=dommy;
}
return *this;
}
//operator-= double
template<class Type>
Vector<Type>& Vector<Type>::operator-=(const Type& dommy)
{
for(int i=0;i<this->vSize;i++)
{
this->vPtr[i]-=dommy;
}
return *this;
}
//operator*= double
template<class Type>
Vector<Type>& Vector<Type>::operator*=(const Type& dommy)
{
for(int i=0;i<this->vSize;i++)
{
this->vPtr[i]*=dommy;
}
return *this;
}
//operator/= double
template<class Type>
Vector<Type>& Vector<Type>::operator/=(const Type& dommy)
{
for(int i=0;i<this->vSize;i++)
{
this->vPtr[i]/=dommy;
}
return *this;
}
//operator--
template<class Type>
Vector<Type>& Vector<Type>::operator--()
{
for(int i=0;i<this->vSize;i++)
this->vPtr[i]--;
return *this;
}
//operator++
template<class Type>
Vector<Type>& Vector<Type>::operator++()
{
for(int i=0;i<this->vSize;i++)
this->vPtr[i]++;
return *this;
}
//operator--
template<class Type>
Vector<Type> Vector<Type>::operator--(int dommy)
{
Vector<Type> temp(*this);
--*this;
return temp;
}
//operator++
template<class Type>
Vector<Type> Vector<Type>::operator++(int dommy)
{
Vector<Type> temp(*this);
++*this;
return temp;
}
template<class Type>
int& Vector<Type>::operator()(int i)const
{
return this->vPtr[i];
}
template<class Type>
int dot(const Vector<Type>& chs,const Vector<Type>& rhs)
{
Type sum = 0;
if(chs.vSize!=rhs.vSize)
return -1;
else
for(int i=0;i<chs.vSize;i++)
{
sum+=chs.vPtr[i]*rhs.vPtr[i];
}
return sum;
}
template<class Type>
void Vector<Type>::normalize ()
{
int temp=0;
for(int i=0;i<this->length();i++)
{
temp+=this->vPtr[i];
}
temp/=this->vSize;
delete this->vPtr;
this->vPtr=new Type[1];
this->vSize=1;
this->vPtr[0]=temp;
}
template<class Type>
double Vector<Type>::length()const
{
return vSize;
}
实在放不下了,driver.cpp
[解决办法]
google刘未鹏的“为什么c++编译器不支持模板的分离编译”
[解决办法]
采取些措施是可以实现分离编译,但事实上是没必要的,还不安全
[解决办法]
模板的声明和实现要放在同一个头文件里面,不然当使用模板时无法解析,这是语言层面上的限制