I'm trying to overload the << operator in this class but this is the output:
Hello,
Segmentation fault: 11
This is my code:
test.cc:
#include <iostream>
#include "class.h"
#include <string>
using namespace std;
int main() {
MYString s("Hello");
MYString s2;
string hello = "Hello";
cout << s.text << ", " << s2.text << endl;
cout << "S: " << s << endl;
hello[0] = 'M';
cout << hello << endl;
return 0;
}
And this is class.h:
#ifndef CLASS_H
#define CLASS_H
#include <string>
using namespace std;
class MYString {
public:
string text;
MYString(string data="") {
text = data;
}
friend ostream& operator << (ostream& os, const MYString& data) {
os << data;
return(os);
}
};
#endif
It compiles fine but I have no idea why it says "Segmentation fault: 11". I have no idea what that means either. Could someone tell me how to fix this? And I'm also really new to C++
(And also I know this code is really pointless but I'm just trying to learn stuff and getting used to C++)