Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help finding the errors in the following code question: The code below contains five (5) syntactic errors under C++17 standard (errors that are

I need help finding the errors in the following code question:

The code below contains five (5) syntactic errors under C++17 standard (errors that are caught by a compiler or generate crashes/memory leaks/undefined behaviour at runtime). Make use of the listed hints when locating errors:

  • Error #1 - In coffee.h related to the use of variable initialization
  • Error #2 - In coffee.h/.cpp related to the use of exceptions
  • Error #3 - In main.cpp related to the use of threads
  • Error #4 - In main.cpp related to the use of pointer initialization
  • Error #5 - In coffee.cpp related to the use of algorithms

coffee.h

// coffee.h #ifndef COFFEE_H #define COFFEE_H #include  #include  #include  extern std::string regions[3]; struct Coffee { std::string name{}; std::string origin{}; std::string flavor{}; std::string roast{}; }; class Roaster { std::list m_flavors; std::list m_roasts; static int timesRoasted; public: Roaster(const std::string& flavors, const std::string& roasts, int len); void operator()(std::list& coffees); static int roastCount() { return timesRoasted; } }; class Cafe { std::list coffees; public: void roastCoffee(Roaster r); Cafe& operator+=(const Coffee& coffee) noexcept; void display() const; }; #endif 

coffee.cpp

#include  #include  #include  #include  #include "coffee.h" // The parameters `flavors` and `roasts` are strings of comma separated // values (e.g., `flavors` could be "Sharp,Delicate,Neutral" // and `roasts` could be "Dark,Light,Medium") // The len parameter is the number of flavors and roasts found // in the strings. For the above example, `len` value is 3. Roaster::Roaster(const std::string& flavors, const std::string& roasts, int len) { int fstartIdx = 0, rstartIdx = 0; for (int i = 0; i < len; ++i) { int fendIdx = flavors.find_first_of(',', fstartIdx); m_flavors.push_back(flavors.substr(fstartIdx, fendIdx - fstartIdx)); fstartIdx = fendIdx + 1; int rendIdx = roasts.find_first_of(',', rstartIdx); m_roasts.push_back(roasts.substr(rstartIdx, rendIdx - rstartIdx)); rstartIdx = rendIdx + 1; } } void Roaster::operator()(std::list& coffees) { auto findRoastForFlavor = [=](const Coffee& c) { std::string theRoast; auto i = m_flavors.begin(); for (; i != m_flavors.end() && theRoast.empty(); i++) { if (c.flavor == *i) theRoast = *i; timesRoasted++; } return theRoast; }; for (auto& c : coffees) { c.roast = findRoastForFlavor(c); } } void Cafe::roastCoffee(Roaster roaster) { roaster(coffees); } Cafe& Cafe::operator+=(const Coffee& coffee) noexcept { auto originCheck = [](const Coffee& coffee) { bool ret = false; for (auto& x : regions) if (coffee.origin == x) ret = true; return ret; }; if (originCheck(coffee)) coffees.push_back(coffee); else throw "Unknown Origin: " + coffee.origin; std::sort(coffees.begin(), coffees.end(), [](Coffee& c1, Coffee& c2) { return c1.name > c2.name; }); return *this; } void Cafe::display() const { std::cout << "Cafe's Stock" << std::endl; for(auto& c : coffees){ std::cout << c.name << ", " << c.origin << ", " << c.flavor << ", " << c.roast << std::endl; } } 

main.cpp

#include  #include  #include  #include  #include "coffee.h" #include "coffee.h" std::string regions[3]{ "Central America", "South America", "Middle East" }; void processCoffee(Cafe& c, Roaster r) { c.roastCoffee(r); } int main() { Coffee coffees[]{ {"Foundry", "Middle East", "Delicate" }, {"Red Brick", "South America", "Sharp" }, {"Dark Star", "Central America", "Neutral" }, {"Lunji Estate", "Canada", "Delicate" } }; std::shared_ptr c1 = new Cafe(); try { ((*c1 += coffees[0]) += coffees[1]); } catch (std::string& msg) { std::cout << msg << std::endl; } Roaster r( "Sharp,Delicate,Neutral" , { "Dark,Light,Medium" }, 3); std::thread t1; t1.join(); t1 = std::thread(processCoffee, std::ref(*c1), r); t1.join(); c1->display(); int rc = r.roastCount(); std::cout << "Roasted #" << rc << " coffees." << 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. Your answer will be evaluated based on clarity of the text and the show of understanding of the concepts that are involved in the error.

Write in the answer box your solution, using the following template:

Error 1:

Error 2:

Error 3:

Error 4:

Error 5:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions