gpt4 book ai didi

使用析构函数删除指针时,C++ Block Type is Valid 错误

转载 作者:行者123 更新时间:2023-11-28 02:19:32 25 4
gpt4 key购买 nike

<分区>

#include <iostream>
#include <string>
using namespace std;

class String
{
private:
int len;
char *str;

public:
String() {}

String(const char *string):len(strlen(string))
{
str = new char[len + 1];
strcpy(str, string);
}

String& operator=(const String& st)
{
len = st.len;
str = new char[len];
strcpy(str, st.str);
return *this;
}

String operator+(const String& obj)
{
char *temp = new char[len + obj.len + 1];
strcpy(temp, str);
strcat(temp, obj.str);
String copy(temp);
delete[]temp;
return copy;
}

~String()
{
delete[] str;
}
};

void main()
{
String str1 = "Hel";
String str2 = "low";
String str3 = str1 + str2;
}

我正在尝试使用析构函数删除 String 类中的指针。

但是我看到了 Block Type Is Valid 的消息。

你能告诉我为什么吗?

而且,我很抱歉对齐不正确,这是我第一次在 stackoverflow 中提问

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com