Hello, I wrote a code but it's crashing when i am using the DESTRUCTOR .
Please help me fixing this :3
My Program has :
1.header file
2.main program
3.header definitions.
Thanks
[code]
#ifndef TEXT_H
#define TEXT_H
#include <iostream>
class Text
{
private:
char *text;
public:
Text();
Text(const Text&);
Text(const char*);
friend std::ostream& operator<<(std::ostream&, const Text&);
void set(const char*);
void set(const Text&);
Text & operator=(const Text & t);
void append(const char*);
void append(const Text &);
int length() const;
bool isEmpty() const;
~Text();
};
#endif
#include "Text.h"
//#include "Menu.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int main()
{
Text t1("Welcome to C++");
Text t2;
Text t3(t1);
cout << "t1:" << t1 << endl;
cout << "t2:" << t2 << endl;
cout << "t3:" << t3 << endl;
t2.set(" Programming");
cout << "t2:" << t2 << endl;
t3.set(t1);
cout << "t3:" << t3 << endl;
t1.append(" Programming");
cout << "t1:" << t1 << endl;
t3.append(t2);
cout << "t3:" << t3 << endl;
_getch();
return 0;
}
#include "Text.h"
#include <string>
#include <iostream>
using namespace std;
Text::Text()
{
this->text = nullptr;
//text = new char();
}
Text::Text(const Text &t)
{
int l = strlen(t.text);
text = new char[l + 1]();
for (int i = 0; i <= l + 1; i++)
{
text[i] = t.text[i];
}
}
Text::Text(const char *t)
{
int l = strlen(t);
text = new char[l + 1];
for (int i = 0; i <= l + 1; i++)
{
text[i] = t[i];
}
}
std::ostream& operator<<(std::ostream& os, const Text& t) {
if (t.text == nullptr) {
os << "";
}
else {
os << t.text;
}
return os;
}
void Text::set(const char *t)
{
int l = strlen(t);
text = new char[l + 1];
for (int i = 0; i <= l + 1; i++) {
text[i] = t[i];
}
}
void Text::set(const Text &t) {
int l = strlen(t.text);
text = new char[l + 1];
for (int i = 0; i <= l + 1; i++) {
text[i] = t.text[i];
}
}
Text& Text::operator=(const Text &t) {
int l = strlen(t.text);
text = new char[l + 1]();
for (int i = 0; i <= l + 1; i++)
{
text[i] = t.text[i];
}
return *this;
}
void Text::append(const char *t)
{
int l = strlen(text), r = strlen(t);
for (int i = 0; i <= r + 1; i++)
{
text[l + i] = t[i];
}
}
void Text::append(const Text &t)
{
int l = strlen(text), r = strlen(t.text);
for (int i = 0; i <= r + 1; i++)
{
text[l + i] = t.text[i];
}
}
int Text::length() const
{
int l = strlen(text);
return l;
}
bool Text::isEmpty() const
{
if (text != nullptr)
{
return false;
}
else
{
return true;
}
}
Text::~Text()
{
cout << "PEHLE" << endl;
if (text!=nullptr)
{
delete[] text;
}
cout << "Baad Mein" << endl;
}
for (int i = 0; i <= l + 1; i++)
{
text[i] = t.text[i];
}
You write to memory that you don't own. If the text contains "Welcome to C++" length = 14 and you allocate correctly 15 chars for the new text. However i <= 15 means you write to text[15] but text[14] is the last you own. Another problem is that you don't delete the old memory before you allocate new memory thus leaking memory.
#include "Text.h"
#include <string>
#include <iostream>
#include <conio.h>
usingnamespace std;
Text::Text()
{
this->text = nullptr;
//text = new char();
}
Text::Text(const Text &t)
{
int l = strlen(t.text);
text = newchar[l + 1]();
for (int i = 0; i <= l ; i++)
{
text[i] = t.text[i];
}
}
Text::Text(constchar *t)
{
int l = strlen(t);
text = newchar[l + 1];
for (int i = 0; i <= l ; i++)
{
text[i] = t[i];
}
}
std::ostream& operator<<(std::ostream& os, const Text& t) {
if (t.text == nullptr) {
os << "";
}
else {
os << t.text;
}
return os;
}
void Text::set(constchar *t)
{
int l = strlen(t);
text = newchar[l + 1];
for (int i = 0; i <= l ; i++) {
text[i] = t[i];
}
}
void Text::set(const Text &t) {
int l = strlen(t.text);
text = newchar[l + 1];
for (int i = 0; i <= l ; i++) {
text[i] = t.text[i];
}
}
Text& Text::operator=(const Text &t) {
int l = strlen(t.text);
text = newchar[l + 1]();
for (int i = 0; i <= l ; i++)
{
text[i] = t.text[i];
}
return *this;
}
void Text::append(constchar *t)
{
char *textcpy;
int l = strlen(text), r = strlen(t);
textcpy = newchar[l + r + 1];
for (int i = 0; i <= l; i++)
{
textcpy[i] = text[i];
}
for (int i = 1; i <= r; i++)
{
textcpy[l + i] = t[i];
}
text = newchar[l + r + 1];
for (int i = 0; i <= l + r; i++) { text[i] = textcpy[i]; }
}
void Text::append(const Text &t)
{
char *textcpy;
int l = strlen(text);
int r = strlen(t.text);
textcpy = newchar[l + r + 1];
for (int i = 0; i <= l; i++)
{
textcpy[i] = text[i];
}
for (int i = 1; i <= r; i++)
{
textcpy[l + i] = t.text[i];
}
text = newchar[l + r + 1];
for (int i = 0; i <= l + r; i++) { text[i] = textcpy[i]; }
}
int Text::length() const
{
int l = strlen(text);
return l;
}
bool Text::isEmpty() const
{
if (text != nullptr)
{
returnfalse;
}
else
{
returntrue;
}
}
Text::~Text()
{
cout << "PEHLE" << endl;
if (text!=nullptr)
{
delete[] text;
}
cout << "Baad Mein" << endl;
}
Now the program don't stand :3 ! i can't see the CMD window once i run the program , it vanish within a sec. :3