【问题标题】:Creating class operator创建类运算符
【发布时间】:2015-10-05 11:17:10
【问题描述】:

尝试创建类操作符:

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'  

如何解决这个问题?

【问题讨论】:

    标签: c++ access-specifier typecast-operator


    【解决方案1】:

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

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

    应该能解决你的问题

    【讨论】:

      猜你喜欢
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 2010-09-11
      • 2013-11-13
      • 1970-01-01
      相关资源
      最近更新 更多