2015-10-05 35 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"; 
    } 

}; 

應該解決您的問題