我正在编写此代码
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
#include <cstddef>
using namespace std;
class MyString
{
public:
MyString(const char ch, int size);
MyString(const char chars[], int size);
char at(int index) const;
int length() const;
MyString append(const MyString& s);
MyString append(int n, char ch);
MyString assign(const MyString& s, int index, int n);
MyString assign(const MyString& s, int n);
MyString assign(int n, char ch);
MyString substr(int index, int n) const;
MyString substr(int index) const;
void clear();
bool empty() const;
private:
char* chars;
int size;
};
MyString::MyString(const char ch, int size)
{
this->size = size;
this->chars[this->size] = ch;
}
MyString::MyString(const char chars[], int size)
{
for(int i = 0; i < size; i++)
{
this->chars[i] = chars[i];
}
this->size = size;
}
char MyString::at(int index) const
{
return chars[index];
}
int MyString::length() const
{
return this->size;
}
MyString MyString::append(const MyString& s)
{
const int newSize = size + s.length();
char *newChars = new char[newSize];
for(int i = 0; i < size; i++)
{
newChars[i] = chars[i];
}
int k = 0;
for(int j = size; j < newSize; j++)
{
newChars[j] = s.chars[k];
k++;
}
MyString myString(newChars, newSize);
return myString;
}
MyString MyString::append(int n, char ch)
{
for(int i = 0; i < n; i++)
{
chars[i] = ch;
}
MyString myString(chars, n);
return myString;
}
MyString MyString::assign(const MyString& s, int index, int n)
{
int j = 0;
const int newSize = n;
for(int i = index; i < index + n; i++)
{
chars[j] = s.chars[i];
j++;
}
MyString myString(chars, newSize);
return myString;
}
MyString MyString::assign(const MyString& s, int n)
{
char *newChars = new char[n];
for(int i = 0; i < n; i++)
{
newChars[i] = s.chars[i];
}
MyString myString(newChars, n);
return myString;
}
MyString MyString::assign(int n, char ch)
{
char *newChars = new char[n];
for(int i = 0; i < n; i++)
{
newChars[i] = ch;
}
MyString myString(newChars, n);
return myString;
}
MyString MyString::substr(int index, int n) const
{
const int newSize = n;
char *newChars = new char[newSize];
int j = 0;
for(int i = index; i < index + n; i++)
{
newChars[j] = chars[i];
j++;
}
MyString myString(newChars, newSize);
return myString;
}
MyString MyString::substr(int index) const
{
const int newSize = size - index;
char *newChars = new char[newSize];
int j = index;
for(int i = 0; i < newSize; i++)
{
newChars[i] = chars[j];
j++;
}
MyString myString(newChars, newSize);
return myString;
}
void MyString::clear() //Clear all the elements in the chars array
{
chars = NULL;
}
bool MyString::empty() const //Check if the chars array is empty
{
if(this->size == 0)
return true;
return false;
}
int main()
{
int size1 = 5;
char chars1[] = {'H', 'E', 'L', 'L', 'O'};
int size2 = 1;
char chars2 = 'A';
MyString str1(chars1, size1);
MyString str2(chars2, size2);
cout << "str1.length() : " << str1.length() << endl;
cout << "str1.at(3) : " << str1.at(3) << endl;
cout << str1.append(str2).length() << endl;
cout << "str1.substr(2)" << str1.substr(2) << endl;
cout << str1.substr(2).at(2) << endl;
return 0;
}
#endif
当我复制它时,它不会显示任何错误。 但是当我运行它时,它显示错误,退出代码3221225477,窗口就像 TestMyString.exe已停止工作。 Windows可以在线检查问题的解决方案。等等......
请帮助我!
答案 0 :(得分:0)
您的基本问题出在MyString
MyString::MyString(const char ch, int size)
{
this->size = size;
this->chars[this->size] = ch;
}
MyString::MyString(const char chars[], int size)
{
for(int i = 0; i < size; i++)
{
this->chars[i] = chars[i];
}
this->size = size;
}
在这两种情况下,chars
的{{1}}成员都是一个指针,构造函数不做任何事情来初始化它,更不用说确保它指向一个有效的内存区域,然后再将数据复制到它。这给出了未定义的行为。
上述结果是MyString
main()
表现出未定义的行为。类似地,在以类似方式构造MyString str1(chars1, size1);
MyString str2(chars2, size2);
的其他地方。