“FATAL ERROR: SIGABRT” 在自定义字符串类中定义字符串构造函数时发生。

huangapple go评论94阅读模式
英文:

"FATAL ERROR: SIGABRT" when defining a string constructor in a custom String class

问题

这个错误发生了

  1. 致命错误:测试用例崩溃:SIGABRT - 中止(异常终止)信号
  2. ::error::错误:以代码 134 和信号 null 退出

当我在测试一个字符串构造函数 "String::String(const char* other)" 时发生的。

  1. String::String(const char* other) // 我说的构造函数
  2. {
  3. if (other == nullptr)
  4. {
  5. String();
  6. }
  7. else if (other != nullptr)
  8. {
  9. int count{0};
  10. for (int i = 0; other[i] != '
    String::String(const char* other) // 我说的构造函数
  11. {
  12.      if (other == nullptr)
  13.      {
  14.           String();
  15.      }
  16.      else if (other != nullptr)
  17.      {
  18.           int count{0};
  19.           for (int i = 0; other[i] != '\0'; ++i)
  20.           {
  21.               count = count + 1;
  22.           }
  23.           slength = count;
  24.           // 删除 [] ifmt; // ?
  25.           ifmt = new char [slength + 1];
  26.           for (int i = 0; i < slength; ++i)    
  27.           {
  28.               ifmt[i] = other[i];
  29.           }
  30.           ifmt[slength] = '\0'; 
  31.      }
  32. }
  33. String::String()
  34. {
  35.     slength = 0; 
  36.     ifmt = new char[slength + 1]; 
  37.     ifmt[0] = '\0';
  38. }
  39. '; ++i)
  40. {
  41. count = count + 1;
  42. }
  43. slength = count;
  44. // 删除 [] ifmt; // ?
  45. ifmt = new char [slength + 1];
  46. for (int i = 0; i < slength; ++i)
  47. {
  48. ifmt[i] = other[i];
  49. }
  50. ifmt[slength] = '
    String::String(const char* other) // 我说的构造函数
  51. {
  52.      if (other == nullptr)
  53.      {
  54.           String();
  55.      }
  56.      else if (other != nullptr)
  57.      {
  58.           int count{0};
  59.           for (int i = 0; other[i] != '\0'; ++i)
  60.           {
  61.               count = count + 1;
  62.           }
  63.           slength = count;
  64.           // 删除 [] ifmt; // ?
  65.           ifmt = new char [slength + 1];
  66.           for (int i = 0; i < slength; ++i)    
  67.           {
  68.               ifmt[i] = other[i];
  69.           }
  70.           ifmt[slength] = '\0'; 
  71.      }
  72. }
  73. String::String()
  74. {
  75.     slength = 0; 
  76.     ifmt = new char[slength + 1]; 
  77.     ifmt[0] = '\0';
  78. }
  79. ';
  80. }
  81. }
  82. String::String()
  83. {
  84. slength = 0;
  85. ifmt = new char[slength + 1];
  86. ifmt[0] = '
    String::String(const char* other) // 我说的构造函数
  87. {
  88.      if (other == nullptr)
  89.      {
  90.           String();
  91.      }
  92.      else if (other != nullptr)
  93.      {
  94.           int count{0};
  95.           for (int i = 0; other[i] != '\0'; ++i)
  96.           {
  97.               count = count + 1;
  98.           }
  99.           slength = count;
  100.           // 删除 [] ifmt; // ?
  101.           ifmt = new char [slength + 1];
  102.           for (int i = 0; i < slength; ++i)    
  103.           {
  104.               ifmt[i] = other[i];
  105.           }
  106.           ifmt[slength] = '\0'; 
  107.      }
  108. }
  109. String::String()
  110. {
  111.     slength = 0; 
  112.     ifmt = new char[slength + 1]; 
  113.     ifmt[0] = '\0';
  114. }
  115. ';
  116. }

在我的自定义 String 类中。

  1. private:
  2. int slength;
  3. 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

  1. FATAL ERROR: test case CRASHED: SIGABRT - Abort (abnormal termination) signal
  2. ::error::Error: Exit with code: 134 and signal: null

when I was testing a string constructor "String::String(const char* other)"

  1. String::String(const char* other) // the constructor I&#39;m talking about
  2. {
  3. if (other == nullptr)
  4. {
  5. String();
  6. }
  7. else if (other != nullptr)
  8. {
  9. int count{0};
  10. for (int i = 0; other[i] != &#39;
    String::String(const char* other) // the constructor I&#39;m talking about
  11. {
  12. if (other == nullptr)
  13. {
  14. String();
  15. }
  16. else if (other != nullptr)
  17. {
  18. int count{0};
  19. for (int i = 0; other[i] != &#39;\0&#39;; ++i)
  20. {
  21. count = count + 1;
  22. }
  23. slength = count;
  24. // delete [] ifmt; // ?
  25. ifmt = new char [slength + 1];
  26. for (int i = 0; i &lt; slength; ++i)    
  27. {
  28. ifmt[i] = other[i];
  29. }
  30. ifmt[slength] = &#39;\0&#39;; 
  31. }
  32. }
  33. }
  34. String::String()
  35. {
  36. slength = 0; 
  37. ifmt = new char[slength + 1]; 
  38. ifmt[0] = &#39;\0&#39;;
  39. }
  40. &#39;; ++i)
  41. {
  42. count = count + 1;
  43. }
  44. slength = count;
  45. // delete [] ifmt; // ?
  46. ifmt = new char [slength + 1];
  47. for (int i = 0; i &lt; slength; ++i)
  48. {
  49. ifmt[i] = other[i];
  50. }
  51. ifmt[slength] = &#39;
    String::String(const char* other) // the constructor I&#39;m talking about
  52. {
  53. if (other == nullptr)
  54. {
  55. String();
  56. }
  57. else if (other != nullptr)
  58. {
  59. int count{0};
  60. for (int i = 0; other[i] != &#39;\0&#39;; ++i)
  61. {
  62. count = count + 1;
  63. }
  64. slength = count;
  65. // delete [] ifmt; // ?
  66. ifmt = new char [slength + 1];
  67. for (int i = 0; i &lt; slength; ++i)    
  68. {
  69. ifmt[i] = other[i];
  70. }
  71. ifmt[slength] = &#39;\0&#39;; 
  72. }
  73. }
  74. }
  75. String::String()
  76. {
  77. slength = 0; 
  78. ifmt = new char[slength + 1]; 
  79. ifmt[0] = &#39;\0&#39;;
  80. }
  81. &#39;;
  82. }
  83. }
  84. }
  85. String::String()
  86. {
  87. slength = 0;
  88. ifmt = new char[slength + 1];
  89. ifmt[0] = &#39;
    String::String(const char* other) // the constructor I&#39;m talking about
  90. {
  91. if (other == nullptr)
  92. {
  93. String();
  94. }
  95. else if (other != nullptr)
  96. {
  97. int count{0};
  98. for (int i = 0; other[i] != &#39;\0&#39;; ++i)
  99. {
  100. count = count + 1;
  101. }
  102. slength = count;
  103. // delete [] ifmt; // ?
  104. ifmt = new char [slength + 1];
  105. for (int i = 0; i &lt; slength; ++i)    
  106. {
  107. ifmt[i] = other[i];
  108. }
  109. ifmt[slength] = &#39;\0&#39;; 
  110. }
  111. }
  112. }
  113. String::String()
  114. {
  115. slength = 0; 
  116. ifmt = new char[slength + 1]; 
  117. ifmt[0] = &#39;\0&#39;;
  118. }
  119. &#39;;
  120. }

in my custom String class.

  1. private:
  2. int slength;
  3. 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,则以下代码段:

  1. if (other == nullptr)
  2. {
  3. String();
  4. }

是没有意义的。

在这个语句中:

  1. String();

会创建一个临时对象,该对象会立即被销毁。

因此,当传递一个空指针时,类的对象会具有未初始化的数据成员。

请注意,这里存在一个多余的右括号:

  1. //...
  2. }
  3. }
  4. String::String()
英文:

This code snippet

  1. if (other == nullptr)
  2. {
  3. String();
  4. }

does not make sense.

In this statement

  1. 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

  1. //...
  2. }
  3. }

String::String()

huangapple
  • 本文由 发表于 2023年2月6日 04:05:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定