I have created class and trying to overload ostream operator using a friend function but my friend is not able access private members of functions. Please help me figure out the problem. I have created another friend function named doSomething() from here I can call private member but not from overloaded function. Please help me updating overloaded stream insertion function.
Class Header File:
#pragma once
#ifndef MYSTRING_H
#define MYSTRING_H
class MyString
{
char* str;
int length;
public:
MyString();
MyString(const char*);
~MyString();
MyString (const MyString &Obj);
void Display() const;
int GetLength() const;
const MyString& operator = (const MyString&);
friend ostream& operator << (ostream &output,MyString&s);
friend void doSomething();
};
#endif
Class CPP File:
#define _CRT_SECURE_NO_WARNINGS
#include "MyString.h"
#include <iostream>
using namespace std;
MyString::MyString() {
length = 0;
str = new char[1];
str[length] = '\0';
}
MyString::MyString(const char* cString) {
length=strlen(cString);
str = new char[length+1];
strcpy(str,cString);
str[length] = '\0';
}
MyString::~MyString() {
delete[] str;
str = nullptr;
length = 0;
}
MyString::MyString(const MyString& Obj) {
length = Obj.length;
str = new char[length+1];
strcpy(str, Obj.str);
str[length] = '\0';
}
void MyString::Display() const {
cout << str << endl;
}
int MyString::GetLength() const {
return length;
}
const MyString& MyString::operator = (const MyString& Obj) {
this->~MyString();
length = Obj.length;
str = new char[length + 1];
strcpy(str, Obj.str);
return *this;
}
Main CPP:
#define _CRT_SECURE_NO_WARNINGS
#include "MyString.h"
#include <iostream>
using namespace std;
ostream &operator << (ostream& output, MyString s) {
output << s.length;
return output;
}
void doSomething() {
MyString s;
s.length;
}
int main() {
return 0;
}