Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pthread linking errors by adding pthread library linkage #9

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
finished cpp exam to 20.
schrodingercatss committed Jan 15, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 9215021bd022996b0909620c5cd952f7f8c70f35
6 changes: 3 additions & 3 deletions exercises/17_class_derive/main.cpp
Original file line number Diff line number Diff line change
@@ -50,9 +50,9 @@ int main(int argc, char **argv) {
B b = B(3);

// TODO: 补全三个类型的大小
static_assert(sizeof(X) == ?, "There is an int in X");
static_assert(sizeof(A) == ?, "There is an int in A");
static_assert(sizeof(B) == ?, "B is an A with an X");
static_assert(sizeof(X) == sizeof(int), "There is an int in X");
static_assert(sizeof(A) == sizeof(int), "There is an int in A");
static_assert(sizeof(B) == sizeof(int) * 2, "B is an A with an X");

i = 0;
std::cout << std::endl
40 changes: 20 additions & 20 deletions exercises/18_class_virtual/main.cpp
Original file line number Diff line number Diff line change
@@ -42,38 +42,38 @@ int main(int argc, char **argv) {
C c;
D d;

ASSERT(a.virtual_name() == '?', MSG);
ASSERT(b.virtual_name() == '?', MSG);
ASSERT(c.virtual_name() == '?', MSG);
ASSERT(d.virtual_name() == '?', MSG);
ASSERT(a.direct_name() == '?', MSG);
ASSERT(b.direct_name() == '?', MSG);
ASSERT(c.direct_name() == '?', MSG);
ASSERT(d.direct_name() == '?', MSG);
ASSERT(a.virtual_name() == 'A', MSG);
ASSERT(b.virtual_name() == 'B', MSG);
ASSERT(c.virtual_name() == 'C', MSG);
ASSERT(d.virtual_name() == 'C', MSG);
ASSERT(a.direct_name() == 'A', MSG);
ASSERT(b.direct_name() == 'B', MSG);
ASSERT(c.direct_name() == 'C', MSG);
ASSERT(d.direct_name() == 'D', MSG);

A &rab = b;
B &rbc = c;
C &rcd = d;

ASSERT(rab.virtual_name() == '?', MSG);
ASSERT(rbc.virtual_name() == '?', MSG);
ASSERT(rcd.virtual_name() == '?', MSG);
ASSERT(rab.direct_name() == '?', MSG);
ASSERT(rbc.direct_name() == '?', MSG);
ASSERT(rcd.direct_name() == '?', MSG);
ASSERT(rab.virtual_name() == 'B', MSG);
ASSERT(rbc.virtual_name() == 'C', MSG);
ASSERT(rcd.virtual_name() == 'C', MSG);
ASSERT(rab.direct_name() == 'A', MSG);
ASSERT(rbc.direct_name() == 'B', MSG);
ASSERT(rcd.direct_name() == 'C', MSG);

A &rac = c;
B &rbd = d;

ASSERT(rac.virtual_name() == '?', MSG);
ASSERT(rbd.virtual_name() == '?', MSG);
ASSERT(rac.direct_name() == '?', MSG);
ASSERT(rbd.direct_name() == '?', MSG);
ASSERT(rac.virtual_name() == 'C', MSG);
ASSERT(rbd.virtual_name() == 'C', MSG);
ASSERT(rac.direct_name() == 'A', MSG);
ASSERT(rbd.direct_name() == 'B', MSG);

A &rad = d;

ASSERT(rad.virtual_name() == '?', MSG);
ASSERT(rad.direct_name() == '?', MSG);
ASSERT(rad.virtual_name() == 'C', MSG);
ASSERT(rad.direct_name() == 'A', MSG);

return 0;
}
28 changes: 16 additions & 12 deletions exercises/19_class_virtual_destruct/main.cpp
Original file line number Diff line number Diff line change
@@ -5,12 +5,12 @@

struct A {
// TODO: 正确初始化静态字段
static int num_a = 0;
static int num_a;

A() {
++num_a;
}
~A() {
virtual ~A() {
--num_a;
}

@@ -20,7 +20,7 @@ struct A {
};
struct B final : public A {
// TODO: 正确初始化静态字段
static int num_b = 0;
static int num_b;

B() {
++num_b;
@@ -34,27 +34,31 @@ struct B final : public A {
}
};

int A::num_a = 0;
int B::num_b = 0;

int main(int argc, char **argv) {
auto a = new A;
auto b = new B;
ASSERT(A::num_a == ?, "Fill in the correct value for A::num_a");
ASSERT(B::num_b == ?, "Fill in the correct value for B::num_b");
ASSERT(a->name() == '?', "Fill in the correct value for a->name()");
ASSERT(b->name() == '?', "Fill in the correct value for b->name()");
ASSERT(A::num_a == 2, "Fill in the correct value for A::num_a");
ASSERT(B::num_b == 1, "Fill in the correct value for B::num_b");
ASSERT(a->name() == 'A', "Fill in the correct value for a->name()");
ASSERT(b->name() == 'B', "Fill in the correct value for b->name()");

delete a;
delete b;
ASSERT(A::num_a == 0, "Every A was destroyed");
ASSERT(B::num_b == 0, "Every B was destroyed");

A *ab = new B;// 派生类指针可以随意转换为基类指针
ASSERT(A::num_a == ?, "Fill in the correct value for A::num_a");
ASSERT(B::num_b == ?, "Fill in the correct value for B::num_b");
ASSERT(ab->name() == '?', "Fill in the correct value for ab->name()");
ASSERT(A::num_a == 1, "Fill in the correct value for A::num_a");
ASSERT(B::num_b == 1, "Fill in the correct value for B::num_b");
ASSERT(ab->name() == 'B', "Fill in the correct value for ab->name()");

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

// TODO: ---- 以下代码不要修改,通过改正类定义解决编译问题 ----
delete ab;// 通过指针可以删除指向的对象,即使是多态对象
11 changes: 8 additions & 3 deletions exercises/20_function_template/main.cpp
Original file line number Diff line number Diff line change
@@ -2,7 +2,12 @@

// READ: 函数模板 <https://zh.cppreference.com/w/cpp/language/function_template>
// TODO: 将这个函数模板化
int plus(int a, int b) {
// int plus(int a, int b) {
// return a + b;
// }

template<typename T>
T plus(T a, T b) {
return a + b;
}

@@ -14,7 +19,7 @@ int main(int argc, char **argv) {
ASSERT(plus(1.25f, 2.5f) == 3.75f, "Plus two float");
ASSERT(plus(1.25, 2.5) == 3.75, "Plus two double");
// TODO: 修改判断条件使测试通过
ASSERT(plus(0.1, 0.2) == 0.3, "How to make this pass?");

// ASSERT(plus(0.1, 0.2) == 0.3, "How to make this pass?");
ASSERT(plus(0.1, 0.2) == 0.1 + 0.2, "How to make this pass?");
return 0;
}