私は最も単純なクラステキストを持っています:
class Text
{
char* txt;
public:
Text(const char*);
Text(const Text&);
~Text();
const Text operator+(const Text&) const;
};
と実装:
#include "text.h"
Text::~Text()
{
delete[] this->txt;
}
Text::Text(const char* argText)
{
txt = new char[strlen(argText)+1];
strcpy(txt, argText);
}
Text::Text(const Text& other)
{
txt = new char[strlen(other.txt)+1];
strcpy(txt, other.txt);
}
const Text Text::operator+(const Text& other) const
{
char* ttxt, *newLine;
ttxt = new char[strlen(txt)+strlen(other.txt)+2];
strcat(ttxt, this->txt);
newLine = new char[2];
newLine[0] = '\n';
newLine[1] = '\0';
strcat(ttxt, newLine);
strcat(ttxt, other.txt);
Text temp(ttxt);
delete[] newLine;
return temp;
}
そしてメイン:
#include "text.h"
int main()
{
Text a("First text.");
Text b("Second lol!!\n kthxbye!!!!!!!");
Text c(a+b);
}
そして、プログラムが中断しますnewLine = new char[2];
new char[5] のように増やしてみましたが、それでも中断します。私が受け取るメッセージは次のとおりです。
Windows は、prTextClass.exe でブレークポイントをトリガーしました。
これは、ヒープの破損が原因である可能性があります。これは、prTextClass.exe または読み込まれた DLL のバグを示しています...