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 #include "Foo.h" using namespace std;

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 #include "Foo.h" using namespace std;

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!