我的代码有问题。我收到 BLOCK_TYPE_IS_VALID 错误...我知道 new 和 delete 有问题,但我找不到它。我有一个带有这些功能的 myString 类:
//constructors and destructors
myString::myString() {
this->string_ = NULL;
this->length = 0;
cout << "created " << "NULL" << endl;
}
myString::myString(char a[]) {
int i;
for (i=0; a[i]!=NULL; i++);
this->length = i;
int j=0;
while(pow(2,j)<i)
j++;
this->string_ = new char [(int)pow(2,j)];
for (int i=0; i<this->length; i++)
this->string_[i] = a[i];
cout << "created " << this->string_ << endl;
}
myString::~myString() {
cout << "deleteing " << this->string_ << endl;
if (this->string_ != NULL)
delete [] this->string_;
}
当我运行这个
myString a("aaa");
myString b("bbbb");
myString c;
c = a + b;
cout << c.get_lenght() << endl;
cout << c.get_string() << endl;
我在“c = a+b”行中得到错误,然后程序停止。