I'm getting this error, I dissected the code even to make sure that I didn't miss a step. I added a extra size when allocating to make sure no memory leakage, I deleted [] . can you guys see what could cause those issues? maybe I am missing something
This is the Error CPP file.
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
#include<string>
#include <fstream>
#include "Error.h"
using namespace std;
namespace sdds {
Error::Error()
{
this->m_message = nullptr;
}
Error::Error(const char* errors) {
this->operator=(errors);
}
int Error::clear() {
delete[] this->m_message;
this->m_message = nullptr;
return 0;
}
Error& Error::operator=(const char* error)
{
if (error == nullptr)
{
this->m_message = nullptr;
}
else
{
delete[] m_message;
int size = strlen(error);
int size2 = size + 1;
m_message = new char[size2];
strcpy(m_message, error);
}
return *this;
}
Error::~Error()
{
clear();
}
Error::operator bool() const {
bool flag3;
if (this->m_message != nullptr)
{
flag3 = true;
}
else {
flag3 = false;
}
return flag3;
}
std::ostream& Error::display(ostream& ostr ) const {
if (this->m_message != nullptr)
{
ostr << this->m_message;
}
return ostr;
}
std::ostream& operator<<(std::ostream& ostr, const Error& right) {
return right.display(ostr);
}
};
This is the header Error File, yes I was told I have bad habit that using namespace std.
``
#ifndef SDDS_ERROR_H
#define SDDS_ERROR_H
#include <iostream>
using namespace std;
namespace sdds
{
class Error {
char *m_message;
public:
Error();
Error(const char* errors);
Error& operator=(const char* error);
operator bool() const;
ostream& display(ostream& ostr=cout) const;
int clear();
~Error();
};
std::ostream& operator<<(std::ostream& ostr, const Error& right);
}
#endif
``
This is the main file
`#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <sstream>
#include "Error.h"
using namespace std;
using namespace sdds;
void line(char fill);
bool T1();
int main() {
line('1');
bool ok = T1();
return 0;
}
bool T1() {
bool passed = true;
Error F;
Error G("Error G has a message");
Error X("Error X has a message too");
Error C = G;
X = G;
if(!F) {
cout << "This is correct >" << F << "<" << endl;
} else {
cout << "This message should not get printed: " << F << endl;
passed = false;
}
if(G) {
cout << "This is correct >" << G << "<" << endl;
} else {
cout << "This message should not get printed: " << G << endl;
passed = false;
}
if(X) {
cout << "This copy assign is correct >" << X << "<" << endl;
} else {
cout << "This message should not get printed: " << X << endl;
passed = false;
}
if(C) {
cout << "This copy is correct >" << C << "<" << endl;
} else {
cout << "This message should not get printed: " << C << endl;
passed = false;
}
G.clear();
if(!G) {
cout << "This is correct >" << G << "<" << endl;
} else {
cout << "This message should not get printed: " << G << endl;
passed = false;
}
return passed;
}
These are all the functions i tried and their errors.I plyed around with the destructor to see what could cause the issue
```
Error::~Error() Leakage
{
m_message = nullptr;
delete[] m_message;
m_message = nullptr;
}
Error::~Error() Segmentaion fault
{
clear();
}
Error::~Error() Segmentaion fault
{
if (m_message != nullptr)
{
clear();
}
}
Error::~Error() Segmentaion fault
{
delete[] m_message;
m_message = nullptr;
}
Error::~Error() Segmentaion fault
{
delete m_message;
m_message = nullptr;
}
Error::~Error() Segmentaion fault
{
delete[] m_message;
delete m_message;
m_message = nullptr;
}