How do you use C++ 20 modules with classes?

Viewed 167

I'm trying to use C++20 modules with classes. But I keep getting this error from the compiler:

collect2.exe: error: ld returned 1 exit status

This the compiler instruction I used to compile the files:

g++ -std=c++20 -fmodules-ts -o main airline_ticket.cpp main.cpp

This is the code for airline_ticket.cpp:

module;
#include <iostream>

export module airline_ticket;

export class AirlineTicket
{
    public:
        AirlineTicket()
        {
            name = "Unknown";
            km = 0;
            isPremium = false;
        };

        int getKm() { return km; };
        void setKm(int distance) { km = distance; };

        std::string getName() { return name; };
        void setName (std::string nameString) { name = nameString; };

        bool getIsPremium() { return isPremium; };
        void setIsPremium(bool status) { isPremium = status; };

        double price()
        {
            if(isPremium)
                return 0;
            else
                return double( getKm() * 0.20 );
        };
    private:
        std::string name;
        int km;
        bool isPremium;
};

And this is the code for main.cpp:

import airline_ticket;

#include <iostream>

int main()
{
    AirlineTicket ticket1;
    ticket1.setName("Leonardo");
    ticket1.setKm(800);
    ticket1.setIsPremium(false);
    double cost { ticket1.price() };
    std::cout << cost << std::endl << ticket1.getKm();
}

Any help would very appreciated.

0 Answers
Related