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) ////////////////////// // Foo.h // Foo.h #ifndef FOO_H #define FOO_H
extern double g_value;
class Foo { long m_id; std::string m_value; Foo* m_theBigFoo{};
public: static int m_shared;
Foo(long id, std::string str = "") : m_id{ id }, m_value{ str } { }
void update(Foo& other) { if (m_id == other.m_id) m_value = m_value + " " + other.m_value; }
std::string getValue() const { return m_value; }
// Enables expressions: Foo + std::string Foo operator+(const Foo& theFoo, std::string val) { return theFoo; } };
// Enables expressions: std::string + Foo std::string operator+(std::string, const Foo&); decltype("" + Foo(3L, "Hello")) process(const Foo&); decltype(Foo(1L, "World") + "") process(Foo&); #endif
////////////////////// // Foo.cpp // Foo.cpp #include
double g_value = 1.2; int Foo::m_shared = 10;
string operator+(string val, const Foo& theFoo)
{ return val; }
decltype("" + Foo(3L, "Hello")) process(const Foo&)
{ return "Hello" + Foo(30L, "World"); }
decltype(Foo(1L, "World") + "") process(Foo&)
{ return Foo(10L, "Hello") + "World"; }
////////////////////// // main.cpp
#include
int main() { cout << ::g_value << " " << Foo::m_shared << endl; { short* ptr = new short[3]; for (auto& item : ptr) { cout << "Item? "; cin >> item; } delete[] ptr; }
{ const Foo aFoo(1, "Midterm"); cout << aFoo.getValue(); aFoo.update(aFoo); cout << process(aFoo).getValue(); }
}
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started