编程原理与实践 3ed 指令中的支持信息不清楚

问题描述 投票:0回答:1

在 Stroustrup 的 PPP 书中,它说我应该在程序的开头添加

#include "PPP.h"
。 所以我去了这本书的网站

他列出的地方

我没有看到关于如何实现这一点的实际解释。

这些是我尝试自己将其组合在一起后从错误列表中得到的错误: Errors from the Error List

“如何使用模块?”链接中,仅简单提及了如何启用模块。我已经完成了:

微软C++

使用 Visual Studio 时。

  • 模块文件后缀为.ixx
  • 设置项目的属性。选项卡:项目 -> 你好项目。
    • 常规属性:将“C++语言标准”设置为“最新”
    • C/C++ 常规:扫描其他模块依赖项:“是”

我已经做了一个测试来检查我是否可以使用模块并且它运行成功,在另一个项目上使用以下内容:

// 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;
}

Console Output: Result: 8

我试图从书中构建但不起作用的程序的结构如下:

// 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

获得了哪些代码

我的项目设置如下所示: Visual Code project Settings, C++ codign standard using 还有这个: Scan sources for Module Dependencies: Yes VC++ Directories:

这些是我从错误列表中得到的错误: Errors from the Error List

从输出窗口:

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>
。但它确实表示建议在本书更深入的练习中使用它。

非常感谢!

c++ visual-studio-2022 header-files c++-modules
1个回答
0
投票

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);
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.