Question: C++ debugging question The code below contains five (5) syntactic errors (errors that are caught by a compiler or generate crashes/undefined behaviour at runtime). (C++17
C++ debugging question
The code below contains five (5) syntactic errors (errors that are caught by a compiler or generate crashes/undefined behaviour at runtime).
(C++17 standard)
(C++17 standard)
01. // Foo.h
02. #ifndef FOO_H
03. #define FOO_H
04.
05. extern double g_value;
06.
07. class Foo
08. {
09. long m_id;
10. std::string m_value;
11.
12. Foo* m_theBigFoo{};
13. public:
14. static int m_shared;
15.
16. Foo(long id, std::string str = "") : m_id{ id }, m_value{ str }
17. {
18. }
19.
20. void update(Foo& other)
21. {
22. if (m_id == other.m_id)
23. m_value = m_value + " " + other.m_value;
24. }
25.
26. std::string getValue() const
27. {
28. return m_value;
29. }
30.
31. // Enables expressions: Foo + std::string
32. Foo operator+(const Foo& theFoo, std::string val)
33. {
34. return theFoo;
35. }
36. };
37.
38. // Enables expressions: std::string + Foo
39. std::string operator+(std::string, const Foo&);
40.
41. decltype("" + Foo(3L, "Hello")) process(const Foo&);
42. decltype(Foo(1L, "World") + "") process(Foo&);
43.
44. #endif
01. // Foo.cpp
02. #include
03. #include "Foo.h"
04. using namespace std;
05.
06. double g_value = 1.2;
07. int Foo::m_shared = 10;
08.
09. string operator+(string val, const Foo& theFoo)
10. {
11. return val;
12. }
13.
14. decltype("" + Foo(3L, "Hello")) process(const Foo&)
15. {
16. return "Hello" + Foo(30L, "World");
17. }
18.
19. decltype(Foo(1L, "World") + "") process(Foo&)
20. {
21. return Foo(10L, "Hello") + "World";
22. }
01. // main.cpp
02. #include
03. #include "Foo.h"
04. using namespace std;
05.
06. int main()
07. {
08. cout << ::g_value << " " << Foo::m_shared << endl;
09. {
10. short* ptr = new short[3];
11. for (auto& item : ptr)
12. {
13. cout << "Item? ";
14. cin >> item;
15. }
16. delete[] ptr;
17. }
18.
19. {
20. const Foo aFoo(1, "Midterm");
21. cout << aFoo.getValue();
22. aFoo.update(aFoo);
23. cout << process(aFoo).getValue();
24. }
25. }
Your task is to identify each one by the file name and line number and explain why the error appears, what C++ standard rule is broken, what C++ feature is misused and how the error should be fixed.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
