module;
#include <iostream>
export module modultest;
export class Test{
public:
Test(){}
void print(){
}
};
I want to create a print
function using cout
, which I need <iostream>
for, but if I include iostream
I get multiple errors, for example:
error: redefinition of 'void operator delete(void*, void*)'
180 | inline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPT { }
I'm using g++ compiler in VSCode.
Per cppreference.com:
Importing modules and header units
...
#include
should not be used in a module unit (outside the global module fragment), because all included declarations and definitions would be considered part of the module. Instead, headers can also be imported as header units with an import declaration:export(optional) import header-name attr(optional);
A header unit is a separate translation unit synthesized from a header. Importing a header unit will make accessible all its definitions and declarations. Preprocessor macros are also accessible (because import declarations are recognized by the preprocessor).
...
So, try replacing #include <iostream>
with import <iostream>;
instead.