I am getting error of inaccessibility even though ive declared them as friends please give a soln to this

#pragma once

#include

#include

class Heystring

{

//non  member operator overloading

friend std::ostream &operator<<(std::ostream,const Heystring &rhs);

friend std::istream &operator>>(std::istream,Heystring &rhs);

 

private:

char *str;

public:

//no args constructor

Heystring();

//args constructor

Heystring(const char *s);

//copy constructor

Heystring(const Heystring &source);

//move constructor

Heystring(Heystring &&source);

//destructor

~Heystring();

//getters&setterss

const char *get_str() const

{

    return str;

} 

int get_len()

{

    return strlen(str);

}

void display() const

{

    std::cout<<str<<" "<<strlen(str)<<std::endl;

}

};

Heystring::Heystring()

{

str=nullptr;

str=new char[1];

str='\0';

}

Heystring::Heystring(const char *s)

{

if(s==nullptr)

{

    str=nullptr;

    str=new char[1];

    str='\0';

}

else

{

    str=nullptr;

    str=new char[strlen(s)+1];

    strcpy(str,s);

}

}

Heystring::Heystring(const Heystring &source)

{

str=nullptr;

str=new char[strlen(source.str)+1];

strcpy(str,source.str);

}

Heystring::Heystring(Heystring &&source)

{

str=source.str;

source.str=nullptr;

std::cout<<"Move assignment called"<<std::endl;

}

Heystring::~Heystring()

{

delete [] str;

}

std::ostream &operator<<(std::ostream &os,const Heystring &rhs)

{

os<<rhs.str;

return os;

}

std::istream &operator>>(std::istream &in,Heystring &rhs)

{

char *buff =new char[1000];

in>>buff;

rhs=Heystring{buff};

delete [] buff;

return in;

}

Please format your code - the forum software has mangled it and it won’t compile! :slight_smile:

Edit:

I’m going to decipher what you’ve posted and make a guess as to what’s wrong: the signatures of your friend declarations:

friend std::ostream &operator<<(std::ostream,const Heystring &rhs);
friend std::istream &operator>>(std::istream,Heystring &rhs);

do not match the signatures of the definitions:

std::ostream &operator<<(std::ostream &os,const Heystring &rhs)
std::istream &operator>>(std::istream &in,Heystring &rhs)

(istream in the friend declaration vs istream& in the definition, etc).

2 Likes

Thanks m8