英文:
"FATAL ERROR: SIGABRT" when defining a string constructor in a custom String class
问题
这个错误发生了
致命错误:测试用例崩溃:SIGABRT - 中止(异常终止)信号
::error::错误:以代码 134 和信号 null 退出
当我在测试一个字符串构造函数 "String::String(const char* other)" 时发生的。
String::String(const char* other) // 我说的构造函数
{
if (other == nullptr)
{
String();
}
else if (other != nullptr)
{
int count{0};
for (int i = 0; other[i] != 'String::String(const char* other) // 我说的构造函数
{
if (other == nullptr)
{
String();
}
else if (other != nullptr)
{
int count{0};
for (int i = 0; other[i] != '\0'; ++i)
{
count = count + 1;
}
slength = count;
// 删除 [] ifmt; // ?
ifmt = new char [slength + 1];
for (int i = 0; i < slength; ++i)
{
ifmt[i] = other[i];
}
ifmt[slength] = '\0';
}
}
String::String()
{
slength = 0;
ifmt = new char[slength + 1];
ifmt[0] = '\0';
}
'; ++i)
{
count = count + 1;
}
slength = count;
// 删除 [] ifmt; // ?
ifmt = new char [slength + 1];
for (int i = 0; i < slength; ++i)
{
ifmt[i] = other[i];
}
ifmt[slength] = 'String::String(const char* other) // 我说的构造函数
{
if (other == nullptr)
{
String();
}
else if (other != nullptr)
{
int count{0};
for (int i = 0; other[i] != '\0'; ++i)
{
count = count + 1;
}
slength = count;
// 删除 [] ifmt; // ?
ifmt = new char [slength + 1];
for (int i = 0; i < slength; ++i)
{
ifmt[i] = other[i];
}
ifmt[slength] = '\0';
}
}
String::String()
{
slength = 0;
ifmt = new char[slength + 1];
ifmt[0] = '\0';
}
';
}
}
String::String()
{
slength = 0;
ifmt = new char[slength + 1];
ifmt[0] = 'String::String(const char* other) // 我说的构造函数
{
if (other == nullptr)
{
String();
}
else if (other != nullptr)
{
int count{0};
for (int i = 0; other[i] != '\0'; ++i)
{
count = count + 1;
}
slength = count;
// 删除 [] ifmt; // ?
ifmt = new char [slength + 1];
for (int i = 0; i < slength; ++i)
{
ifmt[i] = other[i];
}
ifmt[slength] = '\0';
}
}
String::String()
{
slength = 0;
ifmt = new char[slength + 1];
ifmt[0] = '\0';
}
';
}
在我的自定义 String 类中。
private:
int slength;
char* ifmt;
我收到的建议之一是创建一个新的选项分支来处理 "负长度" 并在这种情况下构造一个空字符串,但我不知道什么是 "负长度"。
我还在互联网上搜索了一些案例,但我发现我已经按照他们建议的方式做了,但仍然不起作用。
https://www.geeksforgeeks.org/how-to-create-a-custom-string-class-in-c-with-basic-functionalities/
https://stackoverflow.com/questions/22027697/design-a-string-class-constructor-c
https://stackoverflow.com/questions/4719908/custom-string-exercise-in-c
如果有人能在这种情况下提供一些指导,我将不胜感激。
英文:
This error occurred
FATAL ERROR: test case CRASHED: SIGABRT - Abort (abnormal termination) signal
::error::Error: Exit with code: 134 and signal: null
when I was testing a string constructor "String::String(const char* other)"
String::String(const char* other) // the constructor I'm talking about
{
if (other == nullptr)
{
String();
}
else if (other != nullptr)
{
int count{0};
for (int i = 0; other[i] != 'String::String(const char* other) // the constructor I'm talking about
{
if (other == nullptr)
{
String();
}
else if (other != nullptr)
{
int count{0};
for (int i = 0; other[i] != '\0'; ++i)
{
count = count + 1;
}
slength = count;
// delete [] ifmt; // ?
ifmt = new char [slength + 1];
for (int i = 0; i < slength; ++i)
{
ifmt[i] = other[i];
}
ifmt[slength] = '\0';
}
}
}
String::String()
{
slength = 0;
ifmt = new char[slength + 1];
ifmt[0] = '\0';
}
'; ++i)
{
count = count + 1;
}
slength = count;
// delete [] ifmt; // ?
ifmt = new char [slength + 1];
for (int i = 0; i < slength; ++i)
{
ifmt[i] = other[i];
}
ifmt[slength] = 'String::String(const char* other) // the constructor I'm talking about
{
if (other == nullptr)
{
String();
}
else if (other != nullptr)
{
int count{0};
for (int i = 0; other[i] != '\0'; ++i)
{
count = count + 1;
}
slength = count;
// delete [] ifmt; // ?
ifmt = new char [slength + 1];
for (int i = 0; i < slength; ++i)
{
ifmt[i] = other[i];
}
ifmt[slength] = '\0';
}
}
}
String::String()
{
slength = 0;
ifmt = new char[slength + 1];
ifmt[0] = '\0';
}
';
}
}
}
String::String()
{
slength = 0;
ifmt = new char[slength + 1];
ifmt[0] = 'String::String(const char* other) // the constructor I'm talking about
{
if (other == nullptr)
{
String();
}
else if (other != nullptr)
{
int count{0};
for (int i = 0; other[i] != '\0'; ++i)
{
count = count + 1;
}
slength = count;
// delete [] ifmt; // ?
ifmt = new char [slength + 1];
for (int i = 0; i < slength; ++i)
{
ifmt[i] = other[i];
}
ifmt[slength] = '\0';
}
}
}
String::String()
{
slength = 0;
ifmt = new char[slength + 1];
ifmt[0] = '\0';
}
';
}
in my custom String class.
private:
int slength;
char* ifmt;
One of the suggestions I received was to create a new option branch to handle "negative length" and construct an empty string in this case, but I have no idea what "negative length" is.
I have also searched for some case on the Internet but I found I have done the things they suggested but it still doesn't work.
https://www.geeksforgeeks.org/how-to-create-a-custom-string-class-in-c-with-basic-functionalities/
https://stackoverflow.com/questions/22027697/design-a-string-class-constructor-c
https://stackoverflow.com/questions/4719908/custom-string-exercise-in-c
I will be thankful if anyone could give some guidance in this case.
答案1
得分: 0
这段代码片段中的部分翻译如下:
如果 other 为 nullptr,则以下代码段:
if (other == nullptr)
{
String();
}
是没有意义的。
在这个语句中:
String();
会创建一个临时对象,该对象会立即被销毁。
因此,当传递一个空指针时,类的对象会具有未初始化的数据成员。
请注意,这里存在一个多余的右括号:
//...
}
}
String::String()
英文:
This code snippet
if (other == nullptr)
{
String();
}
does not make sense.
In this statement
String();
there is created a temporary object that at once is destroyed.
As a result when a null pointer is passed then the object of the class has uninitialized data members.
Pay attention to that there is a redundant closing brace
//...
}
}
String::String()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论