2015-10-05 62 views
-3

努力创造一流的运营商:创建一流运营商

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

    }; 

int main() { 

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

} 

,并得到了错误:

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

如何解决这个问题呢?

回答

4

默认情况下,类中的所有成员都是私有的。

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

}; 

应该解决您的问题