Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error defining explicit operator bool() outside class [duplicate]

I have problem defining operator bool() function outside the class

class A{

public:
    explicit operator bool() const; 
};

I am defining the function outside class as...

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

I get this error - error: ‘explicit’ outside class declaration

What is it that am doing wrong?

like image 907
user3922375 Avatar asked Jun 23 '26 08:06

user3922375


1 Answers

Just like you're not supposed to write inline for the definition if you already wrote it for the declaration, you are not allowed to write explicit outside of a class definition:

It may only appear within the decl-specifier-seq of the declaration of such a function within its class definition.

So, just remove it:

/*explicit*/ A::operator bool() const {
    // ...
}
like image 196
Rakete1111 Avatar answered Jun 25 '26 22:06

Rakete1111



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!