I am a C beginner and am looking to create a module. I have followed several guides and would like to test modules with classes. When I try to run the first module via g -11 -c -std=c 20 -fmodules-ts func.cxx
i get the following error:
In file included from /usr/local/Cellar/gcc/11.2.0_3/include/c /11/bits/stl_iterator.h:82,
from /usr/local/Cellar/gcc/11.2.0_3/include/c /11/bits/stl_algobase.h:67,
from /usr/local/Cellar/gcc/11.2.0_3/include/c /11/bits/char_traits.h:39,
from /usr/local/Cellar/gcc/11.2.0_3/include/c /11/string:40,
from func.cxx:2:
/usr/local/Cellar/gcc/11.2.0_3/include/c /11/new:89:27: error: cannot define 'enum class std::align_val_t' in different module
89 | enum class align_val_t: size_t {};
| ^~~~~~
<built-in>: note: declared here
/usr/local/Cellar/gcc/11.2.0_3/include/c /11/new:89: confused by earlier errors, bailing out
Below are the files, thanks in advance.
main.cpp
#include <iostream>
import airline_ticket;
int main()
{
std::cout << "Hello" << std::endl;
return 0;
}
func.cxx
export module airline_ticket;
#include <string>
export class AirlineTicket
{
public:
AirlineTicket();
~AirlineTicket();
double calculatePriceInDollars(); std::string getPassengerName();
void setPassengerName(std::string name);
int getNumberOfMiles();
void setNumberOfMiles(int miles);
bool hasEliteSuperRewardsStatus();
void setHasEliteSuperRewardsStatus(bool status);
private:
std::string m_passengerName;
int m_numberOfMiles;
bool m_hasEliteSuperRewardsStatus;
};
func_impl.cxx
module airline_ticket;
AirlineTicket::AirlineTicket()
{
// Initialize data members.
m_passengerName = "Unknown Passenger";
m_numberOfMiles = 0;
m_hasEliteSuperRewardsStatus = false;
}
CodePudding user response:
the include directive in func.cxx needs to be in the global module fragment area. Else you'll get redefinitions.
I.e.
module;
#include <string>
export module .....
...