提问者:小点点

不能在C++20(Visual Studio)中使用iostream作为模块


无法让此代码在最新版本的MSVC中运行。这个代码示例来自Ivor Horton和Peter Van Weert合著的《从新手到专业人员,开始C++20》一书。

import <iostream>;
int main()
{
    int answer {42};
    std::cout << "The answer to life, universe, and everything is " 
            << answer 
            << std::endl;
    return 0;
}

出现此错误:找不到“C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\Include\IoStream”的头单元

我使用的是Microsoft Visual Studio版本16.8.1,并且在项目属性中启用了这些标志(根据这里类似的问题导入模块的标准方式):

  • /experimental:module
  • /std:C++最新
  • /EHSC
  • /md

有人能帮我吗?我应该改用Clang还是GCC?


共1个答案

匿名用户

尝试这样做:导入;//导入声明

模块有助于将大量代码划分为逻辑部分。模块与命名空间正交。

示例:

Helloworld.cpp

export module helloworld;  // module declaration
import <iostream>;         // import declaration
 
export void hello() {      // export declaration
    std::cout << "Hello world!\n";
}

main.cpp

import helloworld;  // import declaration
 
int main() {
    hello();
}

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(能在|c++20|visual|studio|中|iostream|模块)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?