2

operator bool()在课外定义函数时遇到问题

class A{

public:
    explicit operator bool() const; 
};

我将类外的功能定义为...

explicit A::operator bool() const {
    ...
}

我得到这个错误 -error: ‘explicit’ outside class declaration

做错了什么?

4

1 回答 1

6

就像inline如果你已经为声明编写了定义,你就不应该为定义编写一样,你不能explicit在类定义之外编写:

它只能出现在其类定义中此类函数声明的decl-specifier-seq中。

因此,只需将其删除:

/*explicit*/ A::operator bool() const {
    // ...
}
于 2017-04-27T15:13:42.100 回答