-4

我正在写这段代码

#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 可以在线检查问题的解决方案。呜呜呜……

请帮我!

4

1 回答 1

1

你的基本问题是在构造函数中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成员MyString都是一个指针,构造函数不会对其进行初始化,更不用说在将数据复制到它之前确保它指向有效的内存区域。这给出了未定义的行为。

上面的结果是,在main()

MyString str1(chars1, size1);
MyString str2(chars2, size2);

表现出未定义的行为。MyString在以类似方式构造 a的其他地方也是如此。

于 2016-02-14T00:30:02.170 回答