fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4. #include <unordered_map>
  5. using namespace std;
  6.  
  7. // Manager declaration
  8. class instance;
  9.  
  10. class manager {
  11. public:
  12. instance &get_instance(string name);
  13. void print(string sender, string message);
  14.  
  15. private:
  16. unordered_map<string, unique_ptr<instance>> m_instances;
  17. };
  18.  
  19. // Instance declaration
  20. class instance : public manager {
  21. public:
  22. instance(string name, manager &manager);
  23. void print(string message);
  24.  
  25. private:
  26. string m_name;
  27. manager &m_manager;
  28. };
  29.  
  30. // Manager implementation
  31. instance &manager::get_instance(string name) {
  32. if (m_instances.find(name) == m_instances.end())
  33. m_instances[name] = make_unique<instance>(name, *this);
  34. return *m_instances[name].get();
  35. }
  36.  
  37. void manager::print(string sender, string message) {
  38. cout << sender << ": " << message << endl;
  39. }
  40.  
  41. // Instance implementation
  42. instance::instance(string name, manager &manager) : m_name(name), m_manager(manager) {}
  43.  
  44. void instance::print(string message) {
  45. manager::print(m_name, message);
  46. }
  47.  
  48. int main() {
  49. manager m;
  50. m.get_instance("Foo").print("Bar.");
  51. return 0;
  52. }
  53.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/4.8/unordered_map:35:0,
                 from prog.cpp:4:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
prog.cpp:16:2: error: ‘unordered_map’ does not name a type
  unordered_map<string, unique_ptr<instance>> m_instances;
  ^
prog.cpp: In member function ‘instance& manager::get_instance(std::string)’:
prog.cpp:32:6: error: ‘m_instances’ was not declared in this scope
  if (m_instances.find(name) == m_instances.end())
      ^
prog.cpp:33:23: error: ‘make_unique’ was not declared in this scope
   m_instances[name] = make_unique<instance>(name, *this);
                       ^
prog.cpp:33:43: error: expected primary-expression before ‘>’ token
   m_instances[name] = make_unique<instance>(name, *this);
                                           ^
prog.cpp:33:52: warning: left operand of comma operator has no effect [-Wunused-value]
   m_instances[name] = make_unique<instance>(name, *this);
                                                    ^
prog.cpp:34:10: error: ‘m_instances’ was not declared in this scope
  return *m_instances[name].get();
          ^
prog.cpp:35:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
stdout
Standard output is empty