我试图创建自己的字符串类,但是当我尝试调试代码的一部分时遇到了问题。当我运行调试器时,它会向我显示一条错误消息“正在使用代码03'退出程序”(根据我能够找到的,表示我的系统已运行)的内存),并停止调试器。在调试时我将标记放置在函数的哪个位置并不重要,它只是退出两种方式。但是,如果我只是编译&运行我遇到的程序没有错误。
这是我遇到困难的两个功能:
void MyString::operator += (const MyString& str)
{
if(capacity < (size + str.size + 1))
{
while(capacity < (size + str.size + 1))
{
capacity *= 2;
}
char* newData = new char[capacity];
for (int i = 0; i <= size; ++i)
newData[i] = data[i];
delete [] data;
data = newData;
}
strcat(data, str.data);
size += str.size;
}
MyString MyString::operator + (const MyString& str) const
{
MyString temp(data);
if(temp.capacity < (temp.size + str.size + 1))
{
while(temp.capacity < (temp.size + str.size + 1))
{
temp.capacity *= 2;
}
char* newData = new char[temp.capacity];
for (int i = 0; i <= temp.size; ++i)
newData[i] = temp.data[i];
delete [] temp.data;
temp.data = newData;
}
strcat(temp.data, str.data);
temp.size += str.size;
return temp;
}
这是我的复制构造函数&amp;析构函数,如果它有帮助:
MyString::MyString(const MyString& str)
{
capacity = str.capacity;
size = str.size;
data = new char[capacity];
for(int i = 0; i <= size; ++i)
data[i] = str.data[i];
}
MyString::~MyString()
{
delete [] data;
data = 0;
}
我的代码似乎工作得很好,否则我不一定需要使用调试器来完成它,但我仍然想知道是什么导致调试器以这种奇怪的方式终止。我认为它可能与内存泄漏有关,但我无法弄清楚如何。任何帮助将不胜感激!
编辑: 这是类定义:
#ifndef MY_STRING_H
#define MY_STRING_H
#include <iostream>
class MyString
{
private:
char* data;
int capacity;
int size;
public:
MyString();
MyString(const char*);
~MyString();
MyString(const MyString&);
MyString operator = (const MyString&);
bool operator == (const MyString&) const;
char& operator [ ] (int);
void operator += (const MyString&);
MyString operator + (const MyString&) const;
void getline(istream&, char delimit = ‘\n’);
int length() const;
friend std::ostream& operator<< (std::ostream&, MyString&);
};
#endif
和建设者:
MyString::MyString()
{
size = 0;
capacity = 2;
data = new char[capacity];
data[0] = '\0';
}
MyString::MyString(const char* cStr)
{
size = 0;
while(*(cStr + size) != '\0')
{
++size;
}
capacity = size + 1;
data = new char[capacity];
for(int i = 0; i <= size; ++i)
data[i] = *(cStr + i);
}
EDIT2: 这是主要功能:
#include "MyString.h"
#include <iostream>
using namespace std;
int main()
{
char cStr1[] = {'a','p', 'p', 'l', 'e', '\0'}, cStr2[] = "pie";
MyString str1(cStr1), str2(cStr2), str3;
str3 = str2;
str3 += str1;
cout << "String 1 of length " << str1.length() << " is: " << endl;
cout << str1;
cout << endl;
cout << "String 2 of length " << str2.length() << " is: " << endl;
cout << str2;
cout << endl;
cout << "String 3 of length " << str3.length() << " is: " << endl;
cout << str3;
cout << endl << endl;
cout << "str1[4] is: " << str1[4] << endl << endl;
cout << "Does String 1 and String 3 equal eachother?" << endl;
if(str3 == str1)
cout << "Yes!" << endl;
else
cout << "No." << endl;
cout << "Does (String 1 + String 2) and String 3 equal eachother?" << endl;
if(str3 == (str1 + str2))
cout << "Yes!" << endl;
else
cout << "No." << endl;
cout << "Does (String 2 + String 1) and String 3 equal eachother?" << endl;
if(str3 == (str2 + str1))
cout << "Yes!" << endl;
else
cout << "No." << endl;
return 0;
}
EDIT3: 以下是其他定义:
MyString MyString::operator = (const MyString& str)
{
if (this != &str)
{
size = str.size;
capacity = str.capacity;
char* newData = new char[capacity];
for (int i = 0; i <= size; ++i)
newData[i] = str.data[i];
delete [] data;
data = newData;
}
return *this;
}
bool MyString::operator == (const MyString& str) const
{
if (size != str.size)
return false;
for (int i = 0; i < size; ++i)
{
if (data[i] != str.data[i])
return false;
}
return true;
}
char& MyString::operator [ ] (int position)
{
assert(position >= 0);
assert(position < size);
return data[position];
}
int MyString::length() const
{
return size;
}
ostream& operator<< (ostream& cout, MyString& str)
{
for(int i = 0; i < str.length(); ++i)
cout << str[i];
}