求大神指点,总是debug assertion failed!而且每次运行都不一样,我用的是vc++6.0在win7 64位下
//vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
template <typename T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef vector<T> vecT;
//%%%%%%%%%%%%%%%%%%% constructor && destructor %%%%%%%%%%%%%%%%
vector():theSize(0),theCapacity(0),pt(NULL){}
vector(int n):theSize(n),theCapacity(n) { pt=new T( n ); }
vector(const_iterator first,const_iterator last) //
{
theSize=last-first;
theCapacity=theSize;
pt=new T( theCapacity );
for(int i=0; i<size(); i++)
{
pt[i]=(*(first+i));
cout<<pt[i]<<endl; //这句注释掉后,每次运行都不一样的结果
}
}
vector(const vecT& vec):pt(NULL){ operator=(vec); }
~vector(){cout<<1111<<endl; delete [ ] pt; cout<<2222<<endl;}
//%%%%%%%%%%%%%%%%%%% operator %%%%%%%%%%%%%%%%
const vecT& operator=(const vecT& rhs)
{
if(this != &rhs)
{
theSize=rhs.size();
theCapacity=rhs.capacity();
delete [] pt;
pt=new T( capacity() );
for(int i=0;i<size(); i++)
pt[i]=rhs.pt[i];
}
return *this;
}
T& operator[](int i) { return pt[i]; }
//const T& operator[](int i) const { return pt[i]; }
//%%%%%%%%%%%%%%%%%%% size && capacity && empty %%%%%%%%%%%%%%%%
int size() const {return theSize;}
int capacity()const {return theCapacity;}
int maxsize()const {return unsigned(-1)/sizeof(T);}
bool empty()const {return size()==0;}
private:
int theSize;
int theCapacity;
T* pt;
};
#endif
// main.cpp
#include <iostream>
#include "vector.h"
using namespace std;
void main(){
int a[5]={1,2,3,4,5};
vector<int> vt0;
vector<int> vt1(5);
vector<int> vt2(a,a+5);
vector<int> vt3(vt2);
for(int i=0;i<5;i++)
cout<< vt3[i] <<endl;
}
已找到bug了,括号问题。。。而!已!