断言"str"失败

Assertion `str' failed

本文关键字:失败 str 断言      更新时间:2023-10-16

我是c++的新手。当我覆盖运算符+时,我出现了跟随错误。

ConsoleApplication1.out:/root/projects/ConsoleApplication1/sdstring.cpp:43:静态sdstring::size_t sdstring::strlen(const-char*):断言"str"失败。

这是我的测试代码!

sdstring sd1(NULL);
cout << "sd1:" << sd1 << endl;
sdstring sd2("sd2");
cout << "sd2:" << sd2 << endl;
sdstring sd3;
cin >> sd3;
cout << "sd3:" << sd3 << endl;
sd3 +=sd2 ;
cout << "sd3:" << sd3 << endl;
sdstring sd4 =sd3+sd1;
cout << "sd4:" << sd2 << endl;
sd1 = sd2 + sd1;
cout << "sd1:" << sd1 << endl;
cout << "sd3==sd2:" << (sd3 == sd2)<<endl;

此行发生错误。

sdstring sd4 =sd3+sd1;

这是我的sdstring.cpp文件。

#include "sdstring.h"
#include <assert.h>

sdstring::sdstring(const char *str) {
if (!str) {
datas = new char[1];
datas[0] = '';
}
else {
datas = new char[strlen(str)+1];
strcpy(datas, str);
}
}
sdstring::sdstring(const sdstring& str) {
datas = new char[strlen(str.datas) + 1];
strcpy(datas, str.datas);
}
sdstring& sdstring::operator+(const sdstring& str)const {
sdstring result(NULL);
size_t total_size = getlen() + str.getlen();
result.datas = new char[total_size + 1];
strcpy(result.datas, datas);
strcat(result.datas, str.datas);
return result;
}
bool sdstring::operator==(const sdstring& str)const {
return strcmp(datas, str.datas) == 0;
}
sdstring& sdstring::operator=(const sdstring& str) {
if (this == &str)
return *this;
delete[] datas;
datas = new char[str.getlen() + 1];
strcpy(datas, str.datas);
return *this;
}

sdstring::size_t sdstring::strlen(const char* str) {
assert(str);
size_t len = 0;
while ('' !=  *str++)
len++;
return len;
}
char* sdstring::strcpy( char* des, const char* src){
assert(des&& src);
char* temp = des;
while ('' != (*des++ = *src++));
return temp;
}
int sdstring::strcmp(const char* fir, const char* sec) {
assert(fir  && sec);
while (*fir == *sec)
{
if (*fir == '') {
return 0;
}
++fir;
++sec;
}
return *fir - *sec;
}
char* sdstring::strcat(char* des,const char* src) {
char* temp = des;
while ('' != *des)
{
des++;
}
while ('' != (*des++ = *src++));
return temp;
}
sdstring::~sdstring()
{
if (datas)
{
delete[] datas;
datas = nullptr;
}
}
char& sdstring::operator[](const unsigned int position)const
{
return position < getlen() ? datas[position] : datas[position-1];
}
sdstring& sdstring::operator+=(const sdstring& str)
{
size_t total_size = getlen() + str.getlen();
if (total_size != getlen()) {
char* temp = datas;
datas = new char[total_size + 1];
strcpy(datas,temp);
strcat(datas, str.datas);
delete[] temp;
}
return *this;
}
ostream& operator<<(ostream& os, const sdstring& str)
{
os << str.datas;
return os;
}
istream& operator>>(istream& is, sdstring& str)
{
char* cache = new char[1024];
is >> cache;
delete[]str.datas;
str.datas = new char[sdstring::strlen(cache)];
sdstring::strcpy(str.datas, cache);
delete[]cache;
return is;
}

谁能帮我,首先谢谢!

您的代码有几个问题。然而,operator +是错误的,因为它返回了对局部变量的引用,这是未定义的行为。

sdstring& sdstring::operator+(const sdstring& str)const 
{
sdstring result(NULL);
//..
return result;  // Undefined behavior.
}

operator +应该返回一个全新的对象,而不是对对象的引用。

sdstring sdstring::operator+(const sdstring& str)const // <-- Note the return value is sdstring
{
sdstring result(NULL);
//..
return result;  
}

代码的其他问题:

1)operator=在调用new[]为新字符串分配内存之前使用delete[] datas;破坏内存。如果new[]抛出异常,则sdstring对象将被损坏,因为数据已被销毁,并且您不能返回并使用旧数据重置字符串。使用复制/交换习惯用法可以防止这种情况发生。

2) 应该将字符串的长度存储在成员变量中,而不是每次想要知道字符串的长度时都调用strlenstrlen的速度较慢,因为它必须循环计数每个字符,以确定终止的"\0"字符所在的位置。相反,只需将字符串的长度存储一次并继续使用该值。

3) 不要编写自己的strlenstrcmp函数,而是使用库函数strlenstrcmp。与库版本不同,您的版本未进行优化。

4)operator +可以按照CCD_。operator +应该非常简单地写成:

sdstring sdstring::operator + (const sdstring& rhs)
{
return sdstring(*this) += rhs;
}