Search code examples
c++stringcompiler-errorsstring-literals

How to correctly override literal operators?


Ok, so I made my own class and I've overloaded an operator ""s so I can use it for my string formation.

BUT, I get an error when compiling and I have no idea what it means. Could someone explain the meaning of it and how to fix it?

my code:

PString operator"" s(const char* text, std::size_t len) {
    return PString(std::string(text, len));
}

my error:

error: ‘PString PString::operator""s(const char*, std::size_t)’ must be a non-member function
 PString operator"" s(const char* text, std::size_t len) {

Solution

  • Ok, so prior to asking this question I've been confused about something. Because I've added other operator overrides inside of the class, I thought that I was supposed to add that operator"" s inside of the class as well. But apparently, that is not so.

    I'm keeping this just as a reference to the answer @user0042 gave to me.

    This is what solved the problem for me.