Operator Overloading not working

Hi, when completing the “spaceship operator overloading” section in the 3 part of the c++ series, my compiler kept saying that strong_ordering wasn’t a valid data type. How would I be able to fix this? I think it might have something to do with what c++ version I am running.

Code (Vscode):

#include
#include
using namespace std;

class Length {
public:
explicit Length(int value);
bool operator==(const Length& second) const;
bool operator==(int val) const;
std::strong_ordering operator<=>(const Length& second) const;

private:
int value;
};

Length::Length(int value) : value(value) {}

bool Length::operator==(const Length& second) const {
return (value == second.value);
}
bool Length::operator==(int val) const {
return ((value) == val);
}

std::strong_ordering Length::operator<=>(const Length& second) const{
return value <=> second.value;
}

int main() {
Length first{20};
Length second{20};
return 0;
}

Assuming you pasted your code from length.hpp, length.cpp and main.cpp. Also since your #include do not include any header, make sure you have <compare> header file included.

If everything is correct, then check you language version, it must be C++20 or higher.

You can find more here.