c++6.0运行时总是debug assertion failed,求大神指点!!!

求大神指点,总是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了,括号问题。。。而!已!

a.按F5运行你的程序
b.在出错时,选择“重试”
c.按ALT+7调出“调用栈”窗口
d.双击从上往下的最近一个自己定义的函数,系统会自动把该函数所在的文件显示出来,此时程序就暂停在光标处。一般来说错误就出在这附近。你可以通过查看变量的值来确认
5.可以先声明一个临时的CString变量然后作为暂存,然后把你取得的值放入其中,之后再赋给另一个你要使用的变量,即可解决问题 
6.可能是在编写函数调用指针变量时没有分配空间(如果是在类中定义的指针变量,则指针在多个函数中引用时要分别重新分配空间),检查一下。
7.类似的问题,可能是因为输出对象创建不成功,于是使用的时候用的就是空的对象,于是就出现问题了。
8.“build->clean”
9."Project"->"setting"->"点击c/c++"->"Category选项中选择Preprocessor"
->"在Undefined symbols:填写_DEBUG" 重新编译运行即可
10.我是在做一个ActiveX控件的时候遇到的,由于是初学者,
所以在一个事件处理函数中这些轻率的写:
ClassMyControl pControl;
pControl.f1(); 这些就会出现那个错误,改完Release后虽然异常被屏蔽了,但却直接退出了,解决方法是,声明一个ActiveX控件对象后,还要创建才能正在创建。
pControl.Create(``````);
pControl.f1(); OK!!!
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答