c++c++20clioncoutc++-chrono

C++ Error invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'const std::chrono::year_month_day')


can you help me resolve this error

invalid operands to binary expression ('ostream' (aka 'basic_ostream') and 'const std::chrono::year_month_day')

I compile the source code with CLion 2023.3.2 C++ 20 Apple Silicon M1 clang++ version Apple clang version 15.0.0 (clang-1500.1.0.2.5) Target: arm64-apple-darwin23.2.0 Thread model: posix

Source code

Colonna.h

#ifndef SUPERE_COLONNA_H
#define SUPERE_COLONNA_H

#include <chrono>

namespace colonna {

    class Colonna {
    public:
        Colonna(const std::chrono::year_month_day &dataC);

        const std::chrono::year_month_day &getData() const;

        void setData(const std::chrono::year_month_day &dataC);

    private:
        // Data
        std::chrono::year_month_day dataC;
    };

} // colonna

#endif //SUPERE_COLONNA_H

Colonna.cpp

#include "Colonna.h"

namespace colonna {

    Colonna::Colonna(const std::chrono::year_month_day &dataC) : dataC(dataC) {}

    const std::chrono::year_month_day &Colonna::getData() const {
        return dataC;
    }

    void Colonna::setData(const std::chrono::year_month_day &dataC) {
        Colonna::dataC = dataC;
    }


} // colonna

main.cpp

#include <chrono>
#include <iostream>
#include "Colonna.h"

using namespace colonna;
using namespace std;
using namespace std::chrono;

int main() {

    Colonna colonna ({month(1)/day(12)/year(2024)});
    std::cout << colonna.getData() << '\n';
}

Thank you all

I ran the tests with the IDE and with the following commands

clang++ --std=c++20 main.cpp clang++ --std=gnu++20 main.cpp


Solution

  • The clang version string clang-1500.1.0.2.5 maps to LLVM version 16.0.0.

    Unfortunately std::chrono::operator<<(std::chrono::year_month_day) wasn't added to clang's libc++ until LLVM 17 as can be seen in this demo.