在 Stroustrup 的 PPP 书中,它说我应该在程序的开头添加
#include "PPP.h"
。
所以我去了这本书的网站
他列出的地方
我没有看到关于如何实现这一点的实际解释。
在“如何使用模块?”链接中,仅简单提及了如何启用模块。我已经完成了:
使用 Visual Studio 时。
我已经做了一个测试来检查我是否可以使用模块并且它运行成功,在另一个项目上使用以下内容:
// main.cpp
import test_module;
#include <iostream>
int main() {
int result = add(5, 3);
std::cout << "Result: " << result << std::endl;
return 0;
}
和
// test_module.ixx
export module test_module; // use this syntax
export int add(int a, int b) {
return a + b;
}
我试图从书中构建但不起作用的程序的结构如下:
// ConsoleApplication1.cpp
#include "PPP.h"
int main()
{
cout << "This is simplified compared to the actual program\n";
}
// PPP.h
import PPP;
using namespace PPP;
using namespace std;
// disgusting macro hack to guarantee range checking for []:
#define vector Checked_vector
#define string Checked_string
#define span Checked_span
// PPP.ixx
export module PPP;
export import std;
#define PPP_EXPORT export
#include "PPP_support.h"
using namespace PPP;
那我也有
// PPP_support.h
[...]
我从引用的链接PPP_support.h
获得了哪些代码从输出窗口:
Build started at 7:27 PM...
1>------ Build started: Project: ConsoleApplication1, Configuration: Debug x64 ------
1>Scanning sources for module dependencies...
1>Compiling...
1>PPP.ixx
1>C:\dev\ppp3\ConsoleApplication1\PPP_support.h(86,25): error C2182: 'simple_error': this use of 'void' is not valid
1>(compiling source file 'PPP.ixx')
1>C:\dev\ppp3\ConsoleApplication1\PPP_support.h(86,38): error C2065: 'string': undeclared identifier
1>(compiling source file 'PPP.ixx')
1>C:\dev\ppp3\ConsoleApplication1\PPP_support.h(86,45): error C2146: syntax error: missing ')' before identifier 's'
1>(compiling source file 'PPP.ixx')
1>C:\dev\ppp3\ConsoleApplication1\PPP_support.h(87,2): error C2143: syntax error: missing ';' before '{'
1>(compiling source file 'PPP.ixx')
1>C:\dev\ppp3\ConsoleApplication1\PPP_support.h(87,2): error C2447: '{': missing function header (old-style formal list?)
1>(compiling source file 'PPP.ixx')
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 7:27 PM and took 00.407 seconds ==========
我也尝试过不使用模块(尽管我认为它应该有效,因为我的小程序测试模块 stest 确实有效)并使用“header“PPPheaders.h”当你必须回退到使用头文件时”。但我也没有成功尝试,因为我认为
#define PPP_EXPORT
#include "PPP_support.h"
带我回到起点。
我真的希望有人能帮助我。我这周刚刚买了这本书,非常兴奋,我很期待学习 C++。
我还阅读了其他一些关于同一问题的论坛,但未能找到具体的答案。我现在看到的唯一解决方案是不使用这个“支持”模块/标头,而只是
import std;
或 #include <iostream>
。但它确实表示建议在本书更深入的练习中使用它。
非常感谢!
std 丢失: //PPP_support.h
PPP_EXPORT inline void simple_error(std::string s) // write ``error: s'' and exit program (for non-exception terminating error handling)
{
std::cerr << "error: " << s << '\n';
exit(1);
}