Skip to content

Commit 30da850

Browse files
committed
fix(exercise): 避免缺少头文件和不完整类型问题
Signed-off-by: YdrMaster <[email protected]>
1 parent dba0515 commit 30da850

File tree

5 files changed

+32
-40
lines changed

5 files changed

+32
-40
lines changed

exercises/15_class_derive/main.cpp

+26-36
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,6 @@
22

33
// READ: 派生类 <https://zh.cppreference.com/w/cpp/language/derived_class>
44

5-
// 三个类型的定义在下方,它们的关系是:B 派生自 A 并包含一个 X 类型的成员。
6-
7-
// ↓↓↓ 这是声明
8-
struct X;
9-
struct A;
10-
struct B;
11-
// ↑↑↑ 这是声明
12-
13-
int main(int argc, char **argv) {
14-
X x = X(1);
15-
A a = A(2);
16-
B b = B(3);
17-
18-
// TODO: 补全三个类型的大小
19-
static_assert(sizeof(X) == ?, "There is an int in X");
20-
static_assert(sizeof(A) == ?, "There is an int in A");
21-
static_assert(sizeof(B) == ?, "B is an A with an X");
22-
23-
std::cout << std::endl
24-
<< "-------------------------" << std::endl
25-
<< std::endl;
26-
27-
// 这是不可能的,A 无法提供 B 增加的成员变量的值
28-
// B ba = A(4);
29-
30-
// 这也是不可能的,因为 A 是 B 的一部分,就好像不可以把套娃🪆的外层放进内层里。
31-
A ab = B(5);// 然而这个代码可以编译和运行!
32-
// THINK: 观察打印出的信息,推测把大象放进冰箱分几步?
33-
// THINK: 这样的代码是“安全”的吗?
34-
// NOTICE: 真实场景中不太可能出现这样的代码
35-
36-
return 0;
37-
}
38-
39-
// ↓↓↓ 这是定义
40-
415
struct X {
426
int x;
437

@@ -77,3 +41,29 @@ struct B : public A {
7741
std::cout << "~B(" << a << ", X(" << x.x << "))" << std::endl;
7842
}
7943
};
44+
45+
int main(int argc, char **argv) {
46+
X x = X(1);
47+
A a = A(2);
48+
B b = B(3);
49+
50+
// TODO: 补全三个类型的大小
51+
static_assert(sizeof(X) == ?, "There is an int in X");
52+
static_assert(sizeof(A) == ?, "There is an int in A");
53+
static_assert(sizeof(B) == ?, "B is an A with an X");
54+
55+
std::cout << std::endl
56+
<< "-------------------------" << std::endl
57+
<< std::endl;
58+
59+
// 这是不可能的,A 无法提供 B 增加的成员变量的值
60+
// B ba = A(4);
61+
62+
// 这也是不可能的,因为 A 是 B 的一部分,就好像不可以把套娃🪆的外层放进内层里。
63+
A ab = B(5);// 然而这个代码可以编译和运行!
64+
// THINK: 观察打印出的信息,推测把大象放进冰箱分几步?
65+
// THINK: 这样的代码是“安全”的吗?
66+
// NOTICE: 真实场景中不太可能出现这样的代码
67+
68+
return 0;
69+
}

exercises/17_class_virtual_destruct/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int main(int argc, char **argv) {
5454

5555
// TODO: 基类指针无法随意转换为派生类指针,补全正确的转换语句
5656
B &bb = *ab;
57-
ASSERT(bb->name() == '?', "Fill in the correct value for bb->name()");
57+
ASSERT(bb.name() == '?', "Fill in the correct value for bb->name()");
5858

5959
// TODO: ---- 以下代码不要修改,通过改正类定义解决编译问题 ----
6060
delete ab;// 通过指针可以删除指向的对象,即使是多态对象

exercises/20_class_template/main.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int main(int argc, char **argv) {
4949
auto t0 = Tensor4D(shape, data);
5050
auto t1 = Tensor4D(shape, data);
5151
t0 += t1;
52-
for (auto i = 0u; i < sizeof(data) / sizeof(int); ++i) {
52+
for (auto i = 0u; i < sizeof(data) / sizeof(*data); ++i) {
5353
ASSERT(t0.data[i] == data[i] * 2, "Tensor doubled by plus its self.");
5454
}
5555
}
@@ -80,7 +80,7 @@ int main(int argc, char **argv) {
8080
auto t0 = Tensor4D(s0, d0);
8181
auto t1 = Tensor4D(s1, d1);
8282
t0 += t1;
83-
for (auto i = 0u; i < sizeof(d0) / sizeof(int); ++i) {
83+
for (auto i = 0u; i < sizeof(d0) / sizeof(*d0); ++i) {
8484
ASSERT(t0.data[i] == 7.f, "Every element of t0 should be 7 after adding t1 to it.");
8585
}
8686
}
@@ -102,7 +102,7 @@ int main(int argc, char **argv) {
102102
auto t0 = Tensor4D(s0, d0);
103103
auto t1 = Tensor4D(s1, d1);
104104
t0 += t1;
105-
for (auto i = 0u; i < sizeof(d0) / sizeof(int); ++i) {
105+
for (auto i = 0u; i < sizeof(d0) / sizeof(*d0); ++i) {
106106
ASSERT(t0.data[i] == d0[i] + 1, "Every element of t0 should be incremented by 1 after adding t1 to it.");
107107
}
108108
}

exercises/21_template_const/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "../exercise.h"
2+
#include <cstring>
23

34
// READ: 模板非类型实参 <https://zh.cppreference.com/w/cpp/language/template_parameters#%E6%A8%A1%E6%9D%BF%E9%9D%9E%E7%B1%BB%E5%9E%8B%E5%AE%9E%E5%8F%82>
45

exercises/23_std_vector/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "../exercise.h"
2+
#include <cstring>
23
#include <vector>
34

45
// READ: std::vector <https://zh.cppreference.com/w/cpp/container/vector>

0 commit comments

Comments
 (0)