-3

Trying to create class operator:

class ggg
    {
    int a;
    int b;
    operator std::string ( ) 
        {
        return "hello";
        }

    };

int main() {

    ggg g ;
    std::string s =  g;
    cout<<s;

}

and got error:

'ggg::operator std::string' : cannot access private member declared in class 'ggg'  

How to solve this problem?

risingDarkness
  • 698
  • 12
  • 25
vico
  • 17,051
  • 45
  • 159
  • 315

1 Answers1

4

All members in classes are private by default.

class ggg
{
    int a;
    int b;
public:
    operator std::string ( ) 
    {
        return "hello";
    }

};

should solve your problems

risingDarkness
  • 698
  • 12
  • 25