C++ C++;调试断言失败\u块\u类型\u有效(pHead->;nBlockUse)
我在这里也看过类似的问题,但仍然没有意识到,我做错了什么。请帮忙 我需要为字符串类制作有限大小的模板(如Pascal) 代码如下:C++ C++;调试断言失败\u块\u类型\u有效(pHead->;nBlockUse),c++,c,debugging,assertion,C++,C,Debugging,Assertion,我在这里也看过类似的问题,但仍然没有意识到,我做错了什么。请帮忙 我需要为字符串类制作有限大小的模板(如Pascal) 代码如下: 错误如下:在my_字符串析构函数中,您的代码是: ~my_string () { delete [] this->text; } 您不应该这样做,因为文本是数组,而不是指针。调用*S3=*S1+*S2时,将调用myu字符串运算符+(const myu字符串&s)。在这个操作符中,my_string res是本地
错误如下:在my_字符串析构函数中,您的代码是:
~my_string () {
delete [] this->text;
}
您不应该这样做,因为文本是数组,而不是指针。调用*S3=*S1+*S2
时,将调用myu字符串运算符+(const myu字符串&s)
。在这个操作符中,my_string res
是本地变量并且在堆栈中。此功能结束后,res将自动删除。因此,将res分配给S3是错误的。您可以这样修复:
~my_string () {
}
my_string <max_size, T> operator+(const my_string& s) {
my_string <max_size, T> res;
int count = 0;
while (this->get_char(count) != '\0') {
res.set_char(this->get_char(count), count);
count++;
}
for(int i = 0; count < max_size; i++){
if (s.get_char(i) == '\0') { res.set_char('\0', count); break; }
res.set_char(s.get_char(i), count);
count++;
}
return res;
}
my_string& operator=(const my_string& s){
int size=s.get_length();
for (int i = 0; i < size; i++){
this->set_char(s.get_char(i),i);
}
return *this;
}
首先,构建调试版本并在调试器中运行。调试器将在崩溃位置停止,并允许您将函数调用堆栈升级到代码中。在那里,您将能够检查变量的值。
delete S1;
delete S2;
delete S3;