C++ ostream operator issue - c++

Hi I have this ostream operator that gives me this error when I compile: cannot access private member declared in class 'CService'
here is my code:
friend ostream& operator <<(ostream& os, CService &obj);
ostream& operator<<(ostream &os, CService &obj) {
os<<obj.m_strClient;
return os;
}
I tried returning ostream& but that doesn't fix the problem, instead it adds another error syntax error : ';'
Here is my entire code:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
class CService {
private:
string m_strClient;
string m_strSeller;
int m_iMinutes;
public:
CService() {
m_strClient="N/A";
m_strSeller="N/A";
m_iMinutes=0;
}
CService( string c, string s, int m ) {
m_strClient=c;
m_strSeller=s;
m_iMinutes=m;
}
CService(const CService &obj ) {
m_strClient=obj.m_strClient;
m_strSeller=obj.m_strSeller;
m_iMinutes=obj.m_iMinutes;
}
string GetClient() {
return m_strClient;
}
string GetSeller() {
return m_strSeller;
}
int GetMusic() {
return m_iMinutes;
}
CService CService::operator =(CService obj) {
m_strClient=obj.m_strClient;
m_strSeller=obj.m_strSeller;
m_iMinutes=obj.m_iMinutes;
return *this;
}
bool operator < (const CService &obj) const {
return m_strSeller<obj.m_strSeller;
}
CService CService::operator +(const CService &obj) const {
return CService( m_iMinutes+obj.m_iMinutes );
}
friend ostream& operator <<(ostream& os, CService &obj);
bool CService::operator==(CService &obj) {
return (obj.m_strSeller==m_strSeller);
}
};
ostream& operator<<(ostream &os, CService &obj) {
os<<obj.m_strClient;
return ostream&;
}

Related

C++ cannot access protected member function by friend class

The following example is my demo. At first, I create an abstract class named Expr_node. After that I create another class Int_node. In order to access the private and protected member function normally, I set the Expr class as the friend class of Expr_node. However, I still cannot access to the print function by overload the operator<<.
#include <iostream>
#include <string>
#include <utility>
using namespace std;
class Expr_node;
class Int_node;
class Expr {
friend ostream& operator<<(ostream&, const Expr&);
Expr_node* p;
public:
Expr(int);
Expr(const string&, const Expr&);
Expr(const string&, const Expr&, const Expr&);
Expr(const Expr& t);
Expr& operator=(const Expr&);
};
class Expr_node {
friend ostream& operator<< (ostream&, const Expr_node&);
friend class Expr;
int use;
protected:
Expr_node(): use(1) {}
virtual ~Expr_node() = default;
virtual void print(ostream&) const = 0;
};
ostream& operator<< (ostream& o, const Expr_node& e) {
e.print(o);
return o;
}
ostream& operator<< (ostream& o, const Expr& e) {
e.p->print(o);
return o;
}
class Int_node: public Expr_node {
friend class Expr;
int n;
explicit Int_node(int k) : n(k) {}
void print(ostream& o) const override { o << n;}
};
It's the problem of operator<<. It's not class member, but friend function. Implement function inside Expr class, which call print on p. Then call it on Expr parameter.
Something like this:
#include <iostream>
#include <string>
#include <utility>
using namespace std;
class Expr_node;
class Int_node;
class Expr {
friend ostream& operator<<(ostream&, const Expr&);
Expr_node* p;
public:
Expr(int);
Expr(const string&, const Expr&);
Expr(const string&, const Expr&, const Expr&);
Expr(const Expr& t);
Expr& operator=(const Expr&);
void print(ostream& o) const
{
this->p->print(o);
}
};
class Expr_node {
friend ostream& operator<< (ostream&, const Expr_node&);
friend class Expr;
int use;
protected:
Expr_node(): use(1) {}
virtual ~Expr_node() = default;
virtual void print(ostream&) const = 0;
};
ostream& operator<< (ostream& o, const Expr_node& e) {
e.print(o);
return o;
}
ostream& operator<< (ostream& o, const Expr& e) {
e.print(o);
return o;
}

how to write operator overloading for operator [] in c++

I want to make three[0]='p'; work in the code below. I think I have to make an operator overloading for that but I don't know how to do. What I want to get is to change first index of "Lottery winner!" to 'p'. (To get "pottery winner!").
#include<iostream>
#include<cstring>
using namespace std;
class str
{
char* a;
public:
str(char *aa=""){
this->a = new char[strlen(aa)+1];
strcpy(a,aa);
}
~str(){
delete a;
}
friend ostream& operator<<(ostream &out, str &aa);
friend istream& operator>>(istream &in, str &aa);
};
ostream& operator<<(ostream &out, str &aa){
out<<aa.a;
return out;
}
istream& operator>>(istream &in, str &aa){
in>>aa.a;
return in;
}
void main(){
str three("Lottery winner!");
three[0]='p';
cout<<three<<endl;
}
This is the general signature of the operator[]:
T& operator[](any_type);
In your context it would look like this:
struct str {
...
char& operator[](std::size_t pos) {
return a[pos];
}
};
class str
{
// ...
public:
// ...
char& operator[] (int x)
{
// add array out-of-bounds check here if you like to ...
return a[x];
}
}
operator char*()
{
return a;
}
Could also work

Friend ostream can't access private member

I have seen other questions like this but I didn't get a solution.
Here is the codeļ¼š
cout_overload.h:
#ifndef COUT_OVERLOAD_H_
#define COUT_OVERLOAD_H_
#include <iostream>
class A
{
private:
int data;
public:
A(int d);
friend std::ostream & operator <<
(std::ostream os, const A &t );
};
#endif
cout_overload_r.cpp:
#include <iostream>
#include "cout_overload.h"
A::A(int d)
{
data = d;
}
std::ostream &operator << (std::ostream &os, const A&t)
{
os << " t = " << t.data ;
return os;
}
main.cpp:
#include <iostream> #include "cout_overload.h"
int main(void)
{
A ra(1);
using std::cout;
// cout<<ra;
return 0;
}
the error during compiling:
You need to modify your friend function and Use ostream& inside the
friend std::ostream & operator << (std::ostream os, const A &t );
And replace your above line,
friend std::ostream & operator << (std::ostream &os, const A &t );
Because ostream is an output stream, the & is to pass by reference ( the only way to pass streams to functions )..

Why must I use reference to return/pass std::ostream?

Why I got an error with this code :
ostream operator<<(ostream flux, Perso const A)
{
A.O_Show(flux);
return flux;
}
error: use of deleted function 'std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)'|
And no errors with :
ostream& operator<<(ostream& flux, Perso& const A)
{
A.O_Show(flux);
return flux;
}
Can you explain what's the difference?
As for your code
ostream operator<<(ostream flux, Perso const A) {
A.O_Show(flux);
return flux;
}
You cannot copy a std::ostream as return value (prior to c++11 standards, and even these are protected in 1st place), just change your code to
ostream& operator<<(ostream& flux, Perso& const A) {
// ^
A.O_Show(flux);
return flux;
}

How to overload << operator in inherited class in C++?

My current code is not working.
I am trying to use << operator of Person class in the << operator of Student class. Is that possible?
#include <iostream>
using namespace std;
class Person
{
private:
int EGN;
public:
Person(int e):EGN(e){}
friend ostream& operator <<(ostream& out, const Person& p);
};
ostream& operator <<(ostream& out, const Person& p)
{
out<<p.EGN<<endl;
return out;
}
class Student: public Person
{
private:
int fn;
public:
Student(int e, int f):Person(e)
{
fn=f;
}
friend ostream& operator <<(ostream& out, const Student& p);
};
ostream& operator <<(ostream& out, const Student& p)
{
Person :: operator << (out,p);
out<<p.fn<<endl;
return out;
}
Person's operator << is a friend, not a member, so you can't access it using the :: operator.
Try casting your student to a person to call the right overload:
out << static_cast<const Person &>(p);