module;
#include <iostream>
export module modultest;
export class Test{
public:
Test(){}
void print(){
}
};
我想使用
print
创建一个 cout
函数,我需要 <iostream>
来实现,但是如果我包含 iostream
我会收到多个错误,例如:
error: redefinition of 'void operator delete(void*, void*)'
180 | inline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPT { }
我在 VSCode 中使用 g++ 编译器。
实际上,您的代码是正确的。 Includes需要放入全局模块片段中:
module; // global module fragment
#include <iostream> // correct, inside the global module fragment
export module modultest; // named module declaration
// module purview, owned by module modultest
export class Test {
public:
Test(){}
void print(){
}
};
此代码是正确的,因为
module;
之后的所有内容都位于全局模块片段中,可以在其中包含任何内容。然后,您声明命名模块,之后的任何其他内容都是模块本身的一部分。
您遇到的错误是编译器错误。这是因为 GCC 目前对模块的支持很差。希望您的代码能够与其他编译器或未来版本的 GCC 一起使用。