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
// foo.h class Foo { public: int m_size; double* m_values; char m_str[32];
Foo xyz; int m_cntInst; Foo(int , double); double getValue(int idx) const; ~Foo() { delete m_values; m_values = nullptr; m_size = 0; delete m_str; } }
////////////////////// // 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
// main.cpp #include
int Foo::m_cntInst = 0u;
Foo::Foo(int size, double val) { ++Foo::m_cntInst; m_values = new double[size]; for (int i = 0u; i < size; i++) this.m_values[i] = val; m_size = size; return *this; }
double Foo::getValue(int idx) const { return m_values[idx]; }
const int wordLength = 8; int add(int a, int b){ int xy = 0; return a+b; }
int main(char *argv[]){ Foo vzk(100,55); std::cout << add(500,100) << std::endl; std::cout << xy << std::endl; wordLength++; std::cout << vzk->getValue(50) << std::endl; }
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
