diff --git a/notebooks/04_Classes+Objects.ipynb b/notebooks/04_Classes+Objects.ipynb index 7742c384a03998ab93c6bd02eb495ee8ad90ea9b..73b36093745984e910f33c695739a2647f164628 100644 --- a/notebooks/04_Classes+Objects.ipynb +++ b/notebooks/04_Classes+Objects.ipynb @@ -1 +1 @@ -{"metadata":{"orig_nbformat":4,"language_info":{"codemirror_mode":"text/x-c++src","file_extension":".cpp","mimetype":"text/x-c++src","name":"c++","version":"17"},"kernelspec":{"name":"xcpp17","display_name":"C++17","language":"C++17"}},"nbformat_minor":5,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Classes and Objects\n\nLet's try to implement our first class","metadata":{},"id":"e0e9c863-8720-40a4-a22c-00af773497fe"},{"cell_type":"code","source":"class aggregate {\n short s = 4;\n unsigned int i = 1u<<16;\n double x = 2.0;\n};","metadata":{"trusted":true},"execution_count":1,"outputs":[],"id":"bc685489-cf28-4a15-b0ee-fef2e6143f28"},{"cell_type":"code","source":"#include <iostream>\n\nint main() {\n aggregate agg;\n std::cout << \"s = \" << agg.s << std::endl;\n std::cout << \"i = \" << agg.i << std::endl;\n}","metadata":{"trusted":true},"execution_count":2,"outputs":[{"name":"stderr","text":"input_line_9:3:32: error: 's' is a private member of '__cling_N52::aggregate'\n std::cout << \"s = \" << agg.s << std::endl;\n ^\ninput_line_7:2:11: note: implicitly declared private here\n short s = 4;\n ^\ninput_line_9:4:32: error: 'i' is a private member of '__cling_N52::aggregate'\n std::cout << \"i = \" << agg.i << std::endl;\n ^\ninput_line_7:3:18: note: implicitly declared private here\n unsigned int i = 1u<<16;\n ^\n","output_type":"stream"},{"ename":"Interpreter Error","evalue":"","traceback":["Interpreter Error: "],"output_type":"error"}],"id":"4ada47eb-7158-4caf-aa60-0eb8df7e4769"},{"cell_type":"markdown","source":"That did not work. Why?","metadata":{},"id":"4035a6b6-f751-44fa-a579-a1166df4eed7"},{"cell_type":"markdown","source":"Because object-oriented programming is also about **encapsulation**. By making certain attributes (data members) and method (member functions) invisible to the outside, we\n - can enhance safety: better control over the state of an object\n - make sure that the internal implementation of the class can be changed without breaking outside code \n\nIn C++ we have three levels of access:\n - **public** = any one has access \n - **private** = only the class itself has access\n - **protected** = the class and its children have access\n \nThe difference between a struct and a class lies in their default setting\n\n| | class | struct |\n|:----------:|:-----:|:------:|\n| default is:|private| public |","metadata":{},"id":"159ef323-6f0a-49af-ac06-98804334e5d1"},{"cell_type":"code","source":"class aggregate {\n \n double x; // private by default\n \npublic:\n \n short s = 4;\n unsigned int i = 1u<<16;\n\n};\n\nstruct combo {\n \n int a; // public by default\n \nprivate:\n int internal; // not accessible from outside\n\n};","metadata":{"trusted":true},"execution_count":1,"outputs":[],"id":"7ea6dfba-43fe-4730-b845-4bd49c9b3e3a"},{"cell_type":"code","source":"#include <iostream>\n\nint main() {\n aggregate agg;\n std::cout << \"s = \" << agg.s << std::endl;\n std::cout << \"i = \" << agg.i << std::endl;\n\n combo s;\n s.a = 7;\n}\n\nmain();","metadata":{"trusted":true},"execution_count":2,"outputs":[{"name":"stdout","text":"s = 4\ni = 65536\n","output_type":"stream"}],"id":"6b4cd5c9-0ec3-451b-95b3-e9ad857eea83"},{"cell_type":"markdown","source":"An **object** is an instance of a class; different objects are different entities.","metadata":{},"id":"8c9241eb-1530-42db-8f50-542b8b74bbb8"},{"cell_type":"code","source":"int main() {\n \n aggregate a, b, c;\n \n a.s = 1;\n b.s = 2;\n c.s = 3;\n \n std::cout << \"a's short has a value of \" << a.s << '\\n'\n << \"b's short has a value of \" << b.s << '\\n'\n << \"c's short has a value of \" << c.s << std::endl;\n \n a = c;\n std::cout << \"now a.s = \" << a.s << std::endl;\n}","metadata":{"trusted":true},"execution_count":3,"outputs":[],"id":"2f93bb55-528a-4bac-8336-ccf98cc18af0"},{"cell_type":"code","source":"main();","metadata":{"trusted":true},"execution_count":4,"outputs":[{"name":"stdout","text":"a's short has a value of 1\nb's short has a value of 2\nc's short has a value of 3\nnow a.s = 3\n","output_type":"stream"}],"id":"e5ca8f44-4f3f-4142-8b5e-f5a3c31fee7e"},{"cell_type":"markdown","source":"### Constructors\n\n- Our class *aggregate* used *default member initializers* for setting initial values for its data members.\n- This is one possibility. The other is to use a *constructor* and a *member initializer list*.\n- Constructors are also required, if we want to run some start-up code as part of object creation.","metadata":{},"id":"fe29c0f5-6967-403d-b796-779fe1ccf4be"},{"cell_type":"code","source":"#include <iostream>\n\nclass myClass{\n \n public:\n myClass() : memb_(0) {\n std::cout << \"An instance of myClass was generated.\"\n << \" memb_ = \" << memb_ << std::endl;\n }\n \n private:\n int memb_; // some people adhere to the convention of marking data members by a trailing \"_\"\n}","metadata":{"trusted":true},"execution_count":6,"outputs":[],"id":"4c961b3c-ba83-46e6-aab1-08f1ad4102e3"},{"cell_type":"code","source":"int main() {\n myClass myObj;\n}\n\nmain();","metadata":{"trusted":true},"execution_count":7,"outputs":[{"name":"stdout","text":"An instance of myClass was generated. memb_ = 0\n","output_type":"stream"}],"id":"f501dce2-ef23-4345-8072-45ea85cb8c17"},{"cell_type":"markdown","source":"#### Multiple Constructors\nC++ allows to overload functions. One can use this to implement different constructors.","metadata":{},"id":"5a5ded92-91fc-4a2f-8628-d9531cd84ea2"},{"cell_type":"code","source":"class myClass{\n \n public:\n myClass() : memb_(0) {\n std::cout << \"An instance of myClass was generated.\"\n << \" memb_ = \" << memb_ << std::endl;\n }\n \n myClass( int member ) : memb_(member) {\n std::cout << \"An instance of myClass was generated.\"\n << \" memb_ = \" << memb_ << std::endl;\n }\n \n private:\n int memb_;\n}","metadata":{"trusted":true},"execution_count":8,"outputs":[],"id":"3f4cf1f6-8c50-4057-ab71-259c1046222a"},{"cell_type":"code","source":"int main() {\n myClass myObj1;\n myClass myObj2( 5 );\n}\n\nmain();","metadata":{"trusted":true},"execution_count":5,"outputs":[{"name":"stdout","text":"An instance of myClass was generated. memb_ = 0\nAn instance of myClass was generated. memb_ = 5\n","output_type":"stream"}],"id":"51015ed7-9f4e-40c0-bfa9-2b9f98cd0e1e"},{"cell_type":"markdown","source":"#### Constructor Delegation","metadata":{},"id":"ee95a999-1b5a-4c1b-b2a3-d7dc9a6b306f"},{"cell_type":"markdown","source":"In the example above the body of both constructors is identical. Such a code duplication is, of course, not nice and in violation of the **D**on't **R**epeat **Y**ourself principle. We can avoid this by using **constructor delegation**.","metadata":{},"id":"b977d057-634f-4027-a177-6866bf88160b"},{"cell_type":"code","source":"#include <iostream>\n\nclass myClass{\n \n public:\n myClass( int member ) : memb_(member) {\n std::cout << \"An instance of myClass was generated.\"\n << \" memb_ = \" << memb_ << std::endl;\n }\n\n myClass() : myClass(0) {}\n \n private:\n int memb_;\n};","metadata":{"trusted":true},"execution_count":4,"outputs":[],"id":"8eed6ec9-9ad6-48ab-b0e2-ab0ea35b9b57"},{"cell_type":"markdown","source":"#### Cleaning up: Destructors\nWhen an object goes out of scope and is destroyed we might want or need to do some housekeeping. For this we can implement a destructor.","metadata":{},"id":"fa5b47ea-8d12-4f55-8efe-c08588b81dbc"},{"cell_type":"code","source":"#include <iostream>\n\nclass VerboseClass{\n \n public:\n // c'tor\n VerboseClass() {\n std::cout << \"An instance of VerboseClass was generated.\" << std::endl;\n }\n\n // d'tor\n ~VerboseClass() {\n std::cout << \"An instance of VerboseClass was destroyed.\\n\" << std::endl;\n }\n};","metadata":{"trusted":true},"execution_count":7,"outputs":[],"id":"e456ce69-71e5-4169-b3e8-f302c3f72242"},{"cell_type":"code","source":"int main() {\n for( int k = 0; k < 3; k++ ) {\n VerboseClass talky;\n }\n}\n\nmain();","metadata":{"trusted":true},"execution_count":10,"outputs":[{"name":"stdout","text":"An instance of VerboseClass was generated.\nAn instance of VerboseClass was destroyed.\n\nAn instance of VerboseClass was generated.\nAn instance of VerboseClass was destroyed.\n\nAn instance of VerboseClass was generated.\nAn instance of VerboseClass was destroyed.\n\n","output_type":"stream"}],"id":"8fe56e9c-1842-4c29-84eb-1b20b3f7704a"},{"cell_type":"markdown","source":"#### Small Project: A Vector Class for Linear Algebra\nTasks:\n- We want to implement a class that represents a vector entity from linear algebra.\n- The size of the vector should be selectable at object creation -> need to allocate memory dynamically.\n- Access to vector entries should be 1-based -> operator overloading.\n- Implement simple methods, such as\n - scaling vector with a constant\n - adding two vectors together\n - compute the Euclidean inner product of two vectors","metadata":{},"id":"dffe4043-6959-4169-b0df-203a50d36e0f"},{"cell_type":"code","source":"// --------------------------------------\n// Start with the skeleton of the class\n// --------------------------------------\n\n#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n\n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n};","metadata":{"trusted":true},"execution_count":1,"outputs":[],"id":"f33e5eef-b396-4bfc-8031-f21b33839611"},{"cell_type":"code","source":"int main() {\n VectorClass myVec( 10 );\n}\n\nmain();","metadata":{"trusted":true},"execution_count":2,"outputs":[{"name":"stdout","text":"An instance of VectorClass was generated. dim_ = 10\n","output_type":"stream"}],"id":"af9910d6-a982-437d-9b55-1d0d01998f85"},{"cell_type":"markdown","source":"Our implementation is **missing an essential piece**? What could that be?","metadata":{},"id":"935b6f8d-d347-4edb-9807-f2ed3563dcaf"},{"cell_type":"code","source":"// --------------------------------------\n// Start with the skeleton of the class\n// --------------------------------------\n\n#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n};","metadata":{"trusted":true},"execution_count":6,"outputs":[],"id":"102897af-957e-43b7-a959-eb6ee468f35d"},{"cell_type":"code","source":"int main() {\n VectorClass myVec( 10 );\n}\n\nmain();","metadata":{"trusted":true},"execution_count":7,"outputs":[{"name":"stdout","text":"An instance of VectorClass was generated. dim_ = 10\nAn instance of VectorClass was destroyed and 80 bytes freed.\n","output_type":"stream"}],"id":"adcac578-0bca-4aed-8d13-4514826dd0e7"},{"cell_type":"markdown","source":"Our VectorClass is currently pretty useless, since we cannot manipulate the values of the vector entries. Making vec_ public would break encapsulation. Instead let's\n- overload the [] for providing one-based access\n- add a getter method for the dimension\n- add a member function to pretty-print the vector's entries","metadata":{},"id":"1a74105b-5453-49ab-aeed-e0371809fc52"},{"cell_type":"code","source":"#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ){\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n // pretty print vector to given output stream\n void print( std::ostream &os ) {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};","metadata":{"trusted":true},"execution_count":3,"outputs":[],"id":"a32eb08a-822a-4b83-b3d0-6de281acffba"},{"cell_type":"code","source":"// Let's test our extended class\n\nint main() {\n \n VectorClass myVec( 3 );\n \n myVec[ 1 ] = 3;\n myVec[ 2 ] = 2;\n myVec[ 3 ] = 1;\n \n myVec.print( std::cout );\n}\n\nmain();","metadata":{"trusted":true},"execution_count":4,"outputs":[{"name":"stdout","text":"An instance of VectorClass was generated. dim_ = 3\n(3, 2, 1)\nAn instance of VectorClass was destroyed and 24 bytes freed.\n","output_type":"stream"}],"id":"4eb50e73-21f5-4669-9464-32f11d958732"},{"cell_type":"markdown","source":"**What is left on our todo-list?**\nImplement simple methods, such as\n - scaling vector with a constant\n - adding two vectors together\n - compute the Euclidean inner product of two vectors","metadata":{},"id":"d8af8c97-2855-41f9-bda2-9d5ae341b7c0"},{"cell_type":"code","source":"%%file VectorClass.cpp\n\n#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ){\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n // pretty print vector to given output stream (default will be std::cout)\n void print( std::ostream &os = std::cout ) {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n // scale vector with a constant\n void scale( double factor ) {\n // leave that to students\n }\n \n // add to vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n // for the moment leave it with adding another vector to the one on which the method is called.\n \n // let us try the following, and determine, why it will fail ;-)\n void add( VectorClass other ) {\n\n // make sure that input vector has correct length\n assert( other.getDim() == dim_ );\n // for( unsigned int k = 0; k < dim_; k++ ) {\n // vec_[k] += other[k+1];\n // }\n for( unsigned int k = 1; k <= dim_; k++ ) {\n this->operator[](k) += other[k];\n }\n }\n \n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};\n\n// Modify driver a little bit\nint main() {\n\n const unsigned int dim = 10;\n \n // set up 1st vector\n VectorClass myVec( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n }\n myVec.print( std::cout );\n \n // set up 2nd vector\n VectorClass other( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n other[ idx ] = static_cast<double>( idx );\n }\n other.print( std::cout );\n \n // add the 2nd to the 1st\n myVec.add( other );\n myVec.print();\n}","metadata":{"trusted":true},"execution_count":2,"outputs":[{"name":"stdout","text":"Overwriting VectorClass.cpp\n","output_type":"stream"}],"id":"cdfde2f9-fafc-4a9f-99b7-4952720e4521"},{"cell_type":"code","source":"!g++ -g VectorClass.cpp","metadata":{"trusted":true},"execution_count":3,"outputs":[],"id":"f9331dc3-19ff-44a2-a8f5-6e258f9266d2"},{"cell_type":"code","source":"!./a.out","metadata":{"trusted":true},"execution_count":4,"outputs":[{"name":"stdout","text":"An instance of VectorClass was generated. dim_ = 10\n(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)\nAn instance of VectorClass was generated. dim_ = 10\n(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\nAn instance of VectorClass was destroyed and 80 bytes freed.\n(11, 11, 11, 11, 11, 11, 11, 11, 11, 11)\nfree(): double free detected in tcache 2\nAborted\n","output_type":"stream"}],"id":"4f4d196d-761d-40e1-9d08-5df87307476b"},{"cell_type":"markdown","source":"**Note**: The adding itself seems to have worked correctly, but somehow we still have a problem. So what's wrong here?","metadata":{},"id":"3e04234e-4f33-44c4-9adb-0bde893019df"},{"cell_type":"markdown","source":"The answer to this is multifaceted:\n- Our implementation of *add()* used **call-by-copy**\n- Since we pass an object, its **copy constructor** is invoked\n- We haven't implemented one, but the compiler did automatically (thank's for that ;-)\n- Thus, the copy we get of **other** is a **flat** one!","metadata":{},"id":"7551ddc3-e449-47f1-bdc8-9a8080acdaf9"},{"cell_type":"markdown","source":"***\nWe can check on that by making the destructor tell us which block it is going to deallocate and run our code through valgrind:\n***\n**==> valgrind ./a.out**\n<div>\n==28488== Memcheck, a memory error detector</br>\n==28488== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.</br>\n==28488== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info</br>\n==28488== Command: ./a.out</br>\n==28488== </br>\nAn instance of VectorClass was generated. dim_ = 10</br>\n(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)</br>\nAn instance of VectorClass was generated. dim_ = 10</br>\n(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)</br>\n<font color=\"red\">Going to delete memory starting at address 0x4d9b150</font></br>\nAn instance of VectorClass was destroyed and 80 bytes freed.</br>\n(11, 11, 11, 11, 11, 11, 11, 11, 11, 11)</br>\n<font color=\"red\">Going to delete memory starting at address 0x4d9b150</font></br>\n==28488== Invalid free() / delete / delete[] / realloc()</br>\n==28488== at 0x483758B: operator delete[](void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)</br>\n==28488== by 0x109500: VectorClass::~VectorClass() (VectorClass3.cpp:30)</br>\n==28488== by 0x109341: main (VectorClass3.cpp:93)</br>\n==28488== Address 0x4d9b150 is 0 bytes inside a block of size 80 free'd</br>\n==28488== at 0x483758B: operator delete[](void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)</br>\n==28488== by 0x109500: VectorClass::~VectorClass() (VectorClass3.cpp:30)</br>\n==28488== by 0x109322: main (VectorClass3.cpp:100)</br>\n==28488== Block was alloc'd at</br>\n==28488== at 0x483650F: operator new[](unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)</br>\n==28488== by 0x109450: VectorClass::VectorClass(unsigned int) (VectorClass3.cpp:15)</br>\n==28488== by 0x109291: main (VectorClass3.cpp:93)</br>\n==28488== </br>\nAn instance of VectorClass was destroyed and 80 bytes freed.</br>\nGoing to delete memory starting at address 0x4d9ac80</br>\nAn instance of VectorClass was destroyed and 80 bytes freed.</br>\n==28488== </br>\n==28488== HEAP SUMMARY:</br>\n==28488== in use at exit: 0 bytes in 0 blocks</br>\n==28488== total heap usage: 4 allocs, 5 frees, 73,888 bytes allocated</br>\n==28488== </br>\n==28488== All heap blocks were freed -- no leaks are possible</br>\n==28488== </br>\n==28488== For counts of detected and suppressed errors, rerun with: -v</br>\n==28488== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)</br>\n</div>","metadata":{},"id":"02714f7e-52c4-4890-8a87-05ca91d651e5"},{"cell_type":"markdown","source":"Another way to check on this is to remove the automatic copy constructor","metadata":{},"id":"48d75c49-dd79-420e-9e00-c75865245b5c"},{"cell_type":"code","source":"%%file VectorClass.cpp\n\n#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n\n // Don't allow the following default copy constructor either\n VectorClass( VectorClass &other ) = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ){\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n // pretty print vector to given output stream (default will be std::cout)\n void print( std::ostream &os = std::cout ) {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n // scale vector with a constant\n void scale( double factor ) {\n // leave that to students\n }\n \n // add to vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n // for the moment leave it with adding another vector to the one on which the method is called.\n \n // let us try the following, and determine, why it will fail ;-)\n void add( VectorClass other ) {\n\n // make sure that input vector has correct length\n assert( other.getDim() == dim_ );\n // for( unsigned int k = 0; k < dim_; k++ ) {\n // vec_[k] += other[k+1];\n // }\n for( unsigned int k = 1; k <= dim_; k++ ) {\n this->operator[](k) += other[k];\n }\n }\n \n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};\n\n// Modify driver a little bit\nint main() {\n\n const unsigned int dim = 10;\n \n // set up 1st vector\n VectorClass myVec( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n }\n myVec.print( std::cout );\n \n // set up 2nd vector\n VectorClass other( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n other[ idx ] = static_cast<double>( idx );\n }\n other.print( std::cout );\n \n // add the 2nd to the 1st\n myVec.add( other );\n myVec.print();\n}","metadata":{"trusted":true},"execution_count":1,"outputs":[{"name":"stdout","text":"Overwriting VectorClass.cpp\n","output_type":"stream"}],"id":"def80c0f-89b6-4e00-995a-14b8e800975c"},{"cell_type":"code","source":"!g++ VectorClass.cpp","metadata":{"trusted":true},"execution_count":2,"outputs":[{"name":"stdout","text":"VectorClass.cpp: In function ‘int main()’:\nVectorClass.cpp:101:22: error: use of deleted function ‘VectorClass::VectorClass(VectorClass&)’\n myVec.add( other );\n ^\nVectorClass.cpp:26:9: note: declared here\n VectorClass( VectorClass &other ) = delete;\n ^~~~~~~~~~~\nVectorClass.cpp:63:14: note: initializing argument 1 of ‘void VectorClass::add(VectorClass)’\n void add( VectorClass other ) {\n ^~~\n","output_type":"stream"}],"id":"473de9eb-b04c-4087-8444-8d4b3c4c9fb8"},{"cell_type":"markdown","source":"#### Solution?\nHow should we resolve the issue?\n***\nFor the current case the best solution is to change the interface of our `add()` method to be\n\n`void add( const VectorClass& other )`\n\nThe `const` is no must, but makes it clear to other programmers and the compiler that we are not going to change the object\nother inside add().","metadata":{},"id":"91724843-2e97-4862-888a-291fda71444a"},{"cell_type":"code","source":"#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n\n // Don't allow the following default copy constructor either\n VectorClass( VectorClass &other ) = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ){\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n // pretty print vector to given output stream (default will be std::cout)\n void print( std::ostream &os = std::cout ) {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n // scale vector with a constant\n void scale( double factor ) {\n // leave that to students\n }\n \n // add to vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n // for the moment leave it with adding another vector to the one on which the method is called.\n \n // let us try the following, and determine, why it will fail ;-)\n void add( const VectorClass& other ) {\n\n // make sure that input vector has correct length\n assert( other.getDim() == dim_ );\n // for( unsigned int k = 0; k < dim_; k++ ) {\n // vec_[k] += other[k+1];\n // }\n for( unsigned int k = 1; k <= dim_; k++ ) {\n this->operator[](k) += other[k];\n }\n }\n \n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};\n\n// Modify driver a little bit\nint main() {\n\n const unsigned int dim = 10;\n \n // set up 1st vector\n VectorClass myVec( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n }\n myVec.print( std::cout );\n \n // set up 2nd vector\n VectorClass other( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n other[ idx ] = static_cast<double>( idx );\n }\n other.print( std::cout );\n \n // add the 2nd to the 1st\n myVec.add( other );\n myVec.print();\n}","metadata":{"trusted":true},"execution_count":4,"outputs":[{"name":"stderr","text":"input_line_9:61:45: error: no viable overloaded operator[] for type 'const VectorClass'\n this->operator[](k) += other[k];\n ~~~~~^~\ninput_line_9:32:17: note: candidate function not viable: 'this' argument has type 'const VectorClass', but method is not marked const\n double& operator[] ( unsigned int index ){\n ^\ninput_line_9:71:13: error: function definition is not allowed here\n int main() {\n ^\n","output_type":"stream"},{"ename":"Interpreter Error","evalue":"","traceback":["Interpreter Error: "],"output_type":"error"}],"id":"30eccd22-8c5e-4062-a1d1-8006981aae99"},{"cell_type":"markdown","source":"***\nAh, okay. So the `const` was a good idea, but requires a little bit of extra work!\n\nWe need to implement a second version of the operator overloading that returns a const reference!","metadata":{},"id":"a6bfec6d-9eba-4e34-9ec8-e74bfa485664"},{"cell_type":"code","source":"#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n\n // Don't allow the following default copy constructor either\n VectorClass( VectorClass &other ) = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ){\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n \n // overload the [] for accessing individual entries\n const double& operator[] ( unsigned int index ) {\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n \n // pretty print vector to given output stream (default will be std::cout)\n void print( std::ostream &os = std::cout ) {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n // scale vector with a constant\n void scale( double factor ) {\n // leave that to students\n }\n \n // add to vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n // for the moment leave it with adding another vector to the one on which the method is called.\n \n // let us try the following, and determine, why it will fail ;-)\n void add( const VectorClass& other ) {\n\n // make sure that input vector has correct length\n assert( other.getDim() == dim_ );\n // for( unsigned int k = 0; k < dim_; k++ ) {\n // vec_[k] += other[k+1];\n // }\n for( unsigned int k = 1; k <= dim_; k++ ) {\n this->operator[](k) += other[k];\n }\n }\n \n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};\n\n// Modify driver a little bit\nint main() {\n\n const unsigned int dim = 10;\n \n // set up 1st vector\n VectorClass myVec( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n }\n myVec.print( std::cout );\n \n // set up 2nd vector\n VectorClass other( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n other[ idx ] = static_cast<double>( idx );\n }\n other.print( std::cout );\n \n // add the 2nd to the 1st\n myVec.add( other );\n myVec.print();\n}","metadata":{"trusted":true},"execution_count":2,"outputs":[{"name":"stderr","text":"input_line_10:38:23: error: functions that differ only in their return type cannot be overloaded\n const double& operator[] ( unsigned int index ) {\n ~~~~~~~ ^\ninput_line_10:32:17: note: previous definition is here\n double& operator[] ( unsigned int index ){\n ~~~~~~~ ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, char const (&)[50], void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:14:23: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = char [50]]\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, const unsigned int &, void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:14:78: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = unsigned int]\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n ^\ninput_line_10:14:78: error: use of overloaded operator '<<' is ambiguous (with operand types 'basic_ostream<char, std::char_traits<char> >' and 'unsigned int')\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, char const (&)[46], void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:25:23: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = char [46]]\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n ^\ninput_line_10:25:74: error: use of overloaded operator '<<' is ambiguous (with operand types 'basic_ostream<char, std::char_traits<char> >' and 'unsigned long')\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, char const (&)[2], void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:45:16: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = char [2]]\n os << \"(\";\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, const double &, void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:47:20: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = double]\n os << vec_[k] << \", \";\n ^\ninput_line_10:47:20: error: use of overloaded operator '<<' is ambiguous (with operand types 'std::ostream' (aka 'basic_ostream<char>') and 'double')\n os << vec_[k] << \", \";\n ~~ ^ ~~~~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\ninput_line_10:49:16: error: use of overloaded operator '<<' is ambiguous (with operand types 'std::ostream' (aka 'basic_ostream<char>') and 'double')\n os << vec_[dim_-1] << \")\" << std::endl;\n ~~ ^ ~~~~~~~~~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\ninput_line_10:68:45: error: no viable overloaded operator[] for type 'const VectorClass'\n this->operator[](k) += other[k];\n ~~~~~^~\ninput_line_10:32:17: note: candidate function not viable: 'this' argument has type 'const VectorClass', but method is not marked const\n double& operator[] ( unsigned int index ){\n ^\ninput_line_10:78:13: error: function definition is not allowed here\n int main() {\n ^\nIn file included from input_line_5:1:\nIn file included from /srv/conda/envs/notebook/include/xeus/xinterpreter.hpp:17:\nIn file included from /srv/conda/envs/notebook/include/xeus/xcomm.hpp:19:\nIn file included from /srv/conda/envs/notebook/include/nlohmann/json.hpp:42:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/iterator:64:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:568:8: error: no member named 'setstate' in 'std::basic_ostream<char>'\n __out.setstate(ios_base::badbit);\n ~~~~~ ^\ninput_line_10:14:23: note: in instantiation of function template specialization 'std::operator<<<std::char_traits<char> >' requested here\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n ^\n","output_type":"stream"},{"ename":"Interpreter Error","evalue":"","traceback":["Interpreter Error: "],"output_type":"error"}],"id":"15b02a70-a496-4098-a446-9a60ee30b116"},{"cell_type":"markdown","source":"Shoot, we have a problem with the **signature** now!\n\nBut, fortunately, there is a way out of this:\n- We can tell the compiler that a member function does not alter its object! This will also be part of the signature. So we can implement our overloading as\n`const double& operator[] ( unsigned int index ) const`\n- We might want to do this also for any other member function that does not change the object, like e.g. `print()`\n\nIf we do this, we end up with the final version:","metadata":{},"id":"6a78d290-77dc-43a5-93f5-15fa2c650ddb"},{"cell_type":"code","source":"#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \npublic:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" \n << dim_ << std::endl;\n }\n\n // Don't allow the default constructor\n // [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n\n // Don't allow the following default copy constructor either \n VectorClass( VectorClass &other ) = delete;\n \n // We need to implement a destructor to free the dynamic memory again,\n // otherwise we easily produce memory leaks\n ~VectorClass() {\n std::cout << \"Going to delete memory starting at address \"\n << vec_ << std::endl;\n \n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \"\n << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() const { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ) {\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n const double& operator[] ( unsigned int index ) const {\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n // pretty print vector to given output stream (default will be std::cout)\n void print( std::ostream &os = std::cout ) const {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n // scale vector with a constant\n void scale( double factor ) {\n // leave that to students\n }\n \n // add to vectors together (that's actually a little bit tricky,\n // due to the question \"where to put the result?\"\n // for the moment leave it with adding another vector to the one on which\n // the method is called.\n void add( const VectorClass& other ) {\n\n // make sure that input vector has correct length\n assert( other.getDim() == dim_ );\n // for( unsigned int k = 0; k < dim_; k++ ) {\n // vec_[k] += other[k+1];\n // }\n for( unsigned int k = 1; k <= dim_; k++ ) {\n this->operator[](k) += other[k];\n }\n }\n \nprivate:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};\n\n// Modify driver a little bit\nint main() {\n\n const unsigned int dim = 10;\n \n // set up 1st vector\n VectorClass myVec( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n }\n myVec.print( std::cout );\n \n // set up 2nd vector\n VectorClass other( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n other[ idx ] = static_cast<double>( idx );\n }\n other.print( std::cout );\n \n // add the 2nd to the 1st\n myVec.add( other );\n myVec.print();\n}","metadata":{"trusted":true},"execution_count":2,"outputs":[{"name":"stderr","text":"In file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, char const (&)[50], void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:14:15: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = char [50]]\n std::cout << \"An instance of VectorClass was generated. dim_ = \" \n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, const unsigned int &, void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:15:15: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = unsigned int]\n << dim_ << std::endl;\n ^\ninput_line_10:15:15: error: use of overloaded operator '<<' is ambiguous (with operand types 'basic_ostream<char, std::char_traits<char> >' and 'unsigned int')\n << dim_ << std::endl;\n ^ ~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, char const (&)[44], void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:26:15: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = char [44]]\n std::cout << \"Going to delete memory starting at address \"\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, double *const &, void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:27:15: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = double *]\n << vec_ << std::endl;\n ^\ninput_line_10:27:26: error: reference to overloaded function could not be resolved; did you mean to call it?\n << vec_ << std::endl;\n ^~~~~~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:599:5: note: possible target for call\n endl(basic_ostream<_CharT, _Traits>& __os)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/string_view:580:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>] not viable: no overload of 'endl' matching 'basic_string_view<char, std::char_traits<char> >' for 2nd argument\n operator<<(basic_ostream<_CharT, _Traits>& __os,\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/system_error:217:5: note: candidate function not viable: no overload of 'endl' matching 'const std::error_code' for 2nd argument\n operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:117:7: note: candidate function not viable: no overload of 'endl' matching 'std::basic_ostream<char, std::char_traits<char> >::__ios_type &(*)(std::basic_ostream<char, std::char_traits<char> >::__ios_type &)' (aka 'basic_ios<char, std::char_traits<char> > &(*)(basic_ios<char, std::char_traits<char> > &)') for 1st argument\n operator<<(__ios_type& (*__pf)(__ios_type&))\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:127:7: note: candidate function not viable: no overload of 'endl' matching 'std::ios_base &(*)(std::ios_base &)' for 1st argument\n operator<<(ios_base& (*__pf) (ios_base&))\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function not viable: no overload of 'endl' matching 'long' for 1st argument\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function not viable: no overload of 'endl' matching 'bool' for 1st argument\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function not viable: no overload of 'endl' matching 'short' for 1st argument\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function not viable: no overload of 'endl' matching 'unsigned short' for 1st argument\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function not viable: no overload of 'endl' matching 'int' for 1st argument\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function not viable: no overload of 'endl' matching 'long long' for 1st argument\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function not viable: no overload of 'endl' matching 'unsigned long long' for 1st argument\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function not viable: no overload of 'endl' matching 'float' for 1st argument\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function not viable: no overload of 'endl' matching 'long double' for 1st argument\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:250:7: note: candidate function not viable: no overload of 'endl' matching 'std::nullptr_t' (aka 'nullptr_t') for 1st argument\n operator<<(nullptr_t)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:276:7: note: candidate function not viable: no overload of 'endl' matching 'std::basic_ostream<char, std::char_traits<char> >::__streambuf_type *' (aka 'basic_streambuf<char, std::char_traits<char> > *') for 1st argument\n operator<<(__streambuf_type* __sb);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:506:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>] not viable: no overload of 'endl' matching 'char' for 2nd argument\n operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function not viable: no overload of 'endl' matching 'char' for 2nd argument\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function not viable: no overload of 'endl' matching 'char' for 2nd argument\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function not viable: no overload of 'endl' matching 'signed char' for 2nd argument\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function not viable: no overload of 'endl' matching 'unsigned char' for 2nd argument\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:548:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>] not viable: no overload of 'endl' matching 'const char *' for 2nd argument\n operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:565:5: note: candidate function not viable: no overload of 'endl' matching 'const char *' for 2nd argument\n operator<<(basic_ostream<char, _Traits>& __out, const char* __s)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:578:5: note: candidate function not viable: no overload of 'endl' matching 'const signed char *' for 2nd argument\n operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:583:5: note: candidate function not viable: no overload of 'endl' matching 'const unsigned char *' for 2nd argument\n operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/ostream.tcc:321:5: note: candidate function not viable: no overload of 'endl' matching 'const char *' for 2nd argument\n operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/basic_string.h:6419:5: note: candidate template ignored: couldn't infer template argument '_Alloc'\n operator<<(basic_ostream<_CharT, _Traits>& __os,\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/cstddef:130:5: note: candidate template ignored: couldn't infer template argument '_IntegerType'\n operator<<(byte __b, _IntegerType __shift) noexcept\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:691:5: note: candidate template ignored: couldn't infer template argument '_Tp'\n operator<<(_Ostream&& __os, const _Tp& __x)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/shared_ptr.h:66:5: note: candidate template ignored: couldn't infer template argument '_Tp'\n operator<<(std::basic_ostream<_Ch, _Tr>& __os,\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: couldn't infer template argument '_Dom'\n _DEFINE_EXPR_BINARY_OPERATOR(<<, __shift_left)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:370:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'\n operator _Op(const typename _Dom::value_type& __t, \\\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: couldn't infer template argument '_Dom'\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:396:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'\n operator _Op(const valarray<typename _Dom::value_type>& __v, \\\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1193:1: note: candidate template ignored: couldn't infer template argument '_Tp'\n_DEFINE_BINARY_OPERATOR(<<, __shift_left)\n^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1177:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'\n operator _Op(const typename valarray<_Tp>::value_type& __t, \\\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr' against 'basic_ostream'\n _DEFINE_EXPR_BINARY_OPERATOR(<<, __shift_left)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:344:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'\n operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v, \\\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr' against 'basic_ostream'\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:357:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'\n operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v, \\\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr' against 'basic_ostream'\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:383:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'\n operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e, \\\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1193:1: note: candidate template ignored: could not match 'valarray' against 'basic_ostream'\n_DEFINE_BINARY_OPERATOR(<<, __shift_left)\n^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1155:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'\n operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \\\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1193:1: note: candidate template ignored: could not match 'valarray' against 'basic_ostream'\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1166:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'\n operator _Op(const valarray<_Tp>& __v, \\\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, char const (&)[46], void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:30:15: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = char [46]]\n std::cout << \"An instance of VectorClass was destroyed and \"\n ^\ninput_line_10:31:15: error: use of overloaded operator '<<' is ambiguous (with operand types 'basic_ostream<char, std::char_traits<char> >' and 'unsigned long')\n << dim_ * sizeof(double)\n ^ ~~~~~~~~~~~~~~~~~~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, char const (&)[2], void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:48:8: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = char [2]]\n os << \"(\";\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, const double &, void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:50:10: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = double]\n os << vec_[k] << \", \";\n ^\ninput_line_10:50:10: error: use of overloaded operator '<<' is ambiguous (with operand types 'std::ostream' (aka 'basic_ostream<char>') and 'double')\n os << vec_[k] << \", \";\n ~~ ^ ~~~~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\ninput_line_10:52:8: error: use of overloaded operator '<<' is ambiguous (with operand types 'std::ostream' (aka 'basic_ostream<char>') and 'double')\n os << vec_[dim_-1] << \")\" << std::endl;\n ~~ ^ ~~~~~~~~~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\ninput_line_10:81:13: error: function definition is not allowed here\n int main() {\n ^\nIn file included from input_line_5:1:\nIn file included from /srv/conda/envs/notebook/include/xeus/xinterpreter.hpp:17:\nIn file included from /srv/conda/envs/notebook/include/xeus/xcomm.hpp:19:\nIn file included from /srv/conda/envs/notebook/include/nlohmann/json.hpp:42:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/iterator:64:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:568:8: error: no member named 'setstate' in 'std::basic_ostream<char>'\n __out.setstate(ios_base::badbit);\n ~~~~~ ^\ninput_line_10:14:15: note: in instantiation of function template specialization 'std::operator<<<std::char_traits<char> >' requested here\n std::cout << \"An instance of VectorClass was generated. dim_ = \" \n ^\n","output_type":"stream"},{"ename":"Interpreter Error","evalue":"","traceback":["Interpreter Error: "],"output_type":"error"}],"id":"0b2a852d-2a07-4f2a-85d2-7833051474d7"},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[],"id":"794e3f92-27e2-4362-89d9-d293b6463ee6"}]} \ No newline at end of file +{"metadata":{"orig_nbformat":4,"language_info":{"codemirror_mode":"text/x-c++src","file_extension":".cpp","mimetype":"text/x-c++src","name":"c++","version":"17"},"kernelspec":{"name":"xcpp17","display_name":"C++17","language":"C++17"}},"nbformat_minor":5,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Classes and Objects\n\nLet's try to implement our first class","metadata":{},"id":"e0e9c863-8720-40a4-a22c-00af773497fe"},{"cell_type":"code","source":"class aggregate {\n short s = 4;\n unsigned int i = 1u<<16;\n double x = 2.0;\n};","metadata":{},"execution_count":1,"outputs":[],"id":"bc685489-cf28-4a15-b0ee-fef2e6143f28"},{"cell_type":"code","source":"#include <iostream>\n\nint main() {\n aggregate agg;\n std::cout << \"s = \" << agg.s << std::endl;\n std::cout << \"i = \" << agg.i << std::endl;\n}","metadata":{},"execution_count":2,"outputs":[{"name":"stderr","text":"input_line_9:3:32: error: 's' is a private member of '__cling_N52::aggregate'\n std::cout << \"s = \" << agg.s << std::endl;\n ^\ninput_line_7:2:11: note: implicitly declared private here\n short s = 4;\n ^\ninput_line_9:4:32: error: 'i' is a private member of '__cling_N52::aggregate'\n std::cout << \"i = \" << agg.i << std::endl;\n ^\ninput_line_7:3:18: note: implicitly declared private here\n unsigned int i = 1u<<16;\n ^\n","output_type":"stream"},{"ename":"Interpreter Error","evalue":"","traceback":["Interpreter Error: "],"output_type":"error"}],"id":"4ada47eb-7158-4caf-aa60-0eb8df7e4769"},{"cell_type":"markdown","source":"That did not work. Why?","metadata":{},"id":"4035a6b6-f751-44fa-a579-a1166df4eed7"},{"cell_type":"markdown","source":"Because object-oriented programming is also about **encapsulation**. By making certain attributes (data members) and method (member functions) invisible to the outside, we\n - can enhance safety: better control over the state of an object\n - make sure that the internal implementation of the class can be changed without breaking outside code \n\nIn C++ we have three levels of access:\n - **public** = any one has access \n - **private** = only the class itself has access\n - **protected** = the class and its children have access\n \nThe difference between a struct and a class lies in their default setting\n\n| | class | struct |\n|:----------:|:-----:|:------:|\n| default is:|private| public |","metadata":{},"id":"159ef323-6f0a-49af-ac06-98804334e5d1"},{"cell_type":"code","source":"class aggregate {\n \n double x; // private by default\n \npublic:\n \n short s = 4;\n unsigned int i = 1u<<16;\n\n};\n\nstruct combo {\n \n int a; // public by default\n \nprivate:\n int internal; // not accessible from outside\n\n};","metadata":{},"execution_count":1,"outputs":[],"id":"7ea6dfba-43fe-4730-b845-4bd49c9b3e3a"},{"cell_type":"code","source":"#include <iostream>\n\nint main() {\n aggregate agg;\n std::cout << \"s = \" << agg.s << std::endl;\n std::cout << \"i = \" << agg.i << std::endl;\n\n combo s;\n s.a = 7;\n}\n\nmain();","metadata":{},"execution_count":2,"outputs":[{"name":"stdout","text":"s = 4\ni = 65536\n","output_type":"stream"}],"id":"6b4cd5c9-0ec3-451b-95b3-e9ad857eea83"},{"cell_type":"markdown","source":"An **object** is an instance of a class; different objects are different entities.","metadata":{},"id":"8c9241eb-1530-42db-8f50-542b8b74bbb8"},{"cell_type":"code","source":"int main() {\n \n aggregate a, b, c;\n \n a.s = 1;\n b.s = 2;\n c.s = 3;\n \n std::cout << \"a's short has a value of \" << a.s << '\\n'\n << \"b's short has a value of \" << b.s << '\\n'\n << \"c's short has a value of \" << c.s << std::endl;\n \n a = c;\n std::cout << \"now a.s = \" << a.s << std::endl;\n}","metadata":{},"execution_count":3,"outputs":[],"id":"2f93bb55-528a-4bac-8336-ccf98cc18af0"},{"cell_type":"code","source":"main();","metadata":{},"execution_count":4,"outputs":[{"name":"stdout","text":"a's short has a value of 1\nb's short has a value of 2\nc's short has a value of 3\nnow a.s = 3\n","output_type":"stream"}],"id":"e5ca8f44-4f3f-4142-8b5e-f5a3c31fee7e"},{"cell_type":"markdown","source":"### Constructors\n\n- Our class *aggregate* used *default member initializers* for setting initial values for its data members.\n- This is one possibility. The other is to use a *constructor* and a *member initializer list*.\n- Constructors are also required, if we want to run some start-up code as part of object creation.","metadata":{},"id":"fe29c0f5-6967-403d-b796-779fe1ccf4be"},{"cell_type":"code","source":"#include <iostream>\n\nclass myClass{\n \n public:\n myClass() : memb_(0) {\n std::cout << \"An instance of myClass was generated.\"\n << \" memb_ = \" << memb_ << std::endl;\n }\n \n private:\n int memb_; // some people adhere to the convention of marking data members by a trailing \"_\"\n}","metadata":{},"execution_count":6,"outputs":[],"id":"4c961b3c-ba83-46e6-aab1-08f1ad4102e3"},{"cell_type":"code","source":"int main() {\n myClass myObj;\n}\n\nmain();","metadata":{},"execution_count":7,"outputs":[{"name":"stdout","text":"An instance of myClass was generated. memb_ = 0\n","output_type":"stream"}],"id":"f501dce2-ef23-4345-8072-45ea85cb8c17"},{"cell_type":"markdown","source":"#### Multiple Constructors\nC++ allows to overload functions. One can use this to implement different constructors.","metadata":{},"id":"5a5ded92-91fc-4a2f-8628-d9531cd84ea2"},{"cell_type":"code","source":"class myClass{\n \n public:\n myClass() : memb_(0) {\n std::cout << \"An instance of myClass was generated.\"\n << \" memb_ = \" << memb_ << std::endl;\n }\n \n myClass( int member ) : memb_(member) {\n std::cout << \"An instance of myClass was generated.\"\n << \" memb_ = \" << memb_ << std::endl;\n }\n \n private:\n int memb_;\n}","metadata":{},"execution_count":8,"outputs":[],"id":"3f4cf1f6-8c50-4057-ab71-259c1046222a"},{"cell_type":"code","source":"int main() {\n myClass myObj1;\n myClass myObj2( 5 );\n}\n\nmain();","metadata":{},"execution_count":5,"outputs":[{"name":"stdout","text":"An instance of myClass was generated. memb_ = 0\nAn instance of myClass was generated. memb_ = 5\n","output_type":"stream"}],"id":"51015ed7-9f4e-40c0-bfa9-2b9f98cd0e1e"},{"cell_type":"markdown","source":"#### Constructor Delegation","metadata":{},"id":"ee95a999-1b5a-4c1b-b2a3-d7dc9a6b306f"},{"cell_type":"markdown","source":"In the example above the body of both constructors is identical. Such a code duplication is, of course, not nice and in violation of the **D**on't **R**epeat **Y**ourself principle. We can avoid this by using **constructor delegation**.","metadata":{},"id":"b977d057-634f-4027-a177-6866bf88160b"},{"cell_type":"code","source":"#include <iostream>\n\nclass myClass{\n \n public:\n myClass( int member ) : memb_(member) {\n std::cout << \"An instance of myClass was generated.\"\n << \" memb_ = \" << memb_ << std::endl;\n }\n\n myClass() : myClass(0) {}\n \n private:\n int memb_;\n};","metadata":{},"execution_count":4,"outputs":[],"id":"8eed6ec9-9ad6-48ab-b0e2-ab0ea35b9b57"},{"cell_type":"markdown","source":"#### Cleaning up: Destructors\nWhen an object goes out of scope and is destroyed we might want or need to do some housekeeping. For this we can implement a destructor.","metadata":{},"id":"fa5b47ea-8d12-4f55-8efe-c08588b81dbc"},{"cell_type":"code","source":"#include <iostream>\n\nclass VerboseClass{\n \n public:\n // c'tor\n VerboseClass() {\n std::cout << \"An instance of VerboseClass was generated.\" << std::endl;\n }\n\n // d'tor\n ~VerboseClass() {\n std::cout << \"An instance of VerboseClass was destroyed.\\n\" << std::endl;\n }\n};","metadata":{},"execution_count":7,"outputs":[],"id":"e456ce69-71e5-4169-b3e8-f302c3f72242"},{"cell_type":"code","source":"int main() {\n for( int k = 0; k < 3; k++ ) {\n VerboseClass talky;\n }\n}\n\nmain();","metadata":{},"execution_count":10,"outputs":[{"name":"stdout","text":"An instance of VerboseClass was generated.\nAn instance of VerboseClass was destroyed.\n\nAn instance of VerboseClass was generated.\nAn instance of VerboseClass was destroyed.\n\nAn instance of VerboseClass was generated.\nAn instance of VerboseClass was destroyed.\n\n","output_type":"stream"}],"id":"8fe56e9c-1842-4c29-84eb-1b20b3f7704a"},{"cell_type":"markdown","source":"#### Small Project: A Vector Class for Linear Algebra\nTasks:\n- We want to implement a class that represents a vector entity from linear algebra.\n- The size of the vector should be selectable at object creation -> need to allocate memory dynamically.\n- Access to vector entries should be 1-based -> operator overloading.\n- Implement simple methods, such as\n - scaling vector with a constant\n - adding two vectors together\n - compute the Euclidean inner product of two vectors","metadata":{},"id":"dffe4043-6959-4169-b0df-203a50d36e0f"},{"cell_type":"code","source":"// --------------------------------------\n// Start with the skeleton of the class\n// --------------------------------------\n\n#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n\n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n};","metadata":{},"execution_count":1,"outputs":[],"id":"f33e5eef-b396-4bfc-8031-f21b33839611"},{"cell_type":"code","source":"int main() {\n VectorClass myVec( 10 );\n}\n\nmain();","metadata":{},"execution_count":2,"outputs":[{"name":"stdout","text":"An instance of VectorClass was generated. dim_ = 10\n","output_type":"stream"}],"id":"af9910d6-a982-437d-9b55-1d0d01998f85"},{"cell_type":"markdown","source":"Our implementation is **missing an essential piece**? What could that be?","metadata":{},"id":"935b6f8d-d347-4edb-9807-f2ed3563dcaf"},{"cell_type":"code","source":"// --------------------------------------\n// Start with the skeleton of the class\n// --------------------------------------\n\n#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n};","metadata":{},"execution_count":6,"outputs":[],"id":"102897af-957e-43b7-a959-eb6ee468f35d"},{"cell_type":"code","source":"int main() {\n VectorClass myVec( 10 );\n}\n\nmain();","metadata":{},"execution_count":7,"outputs":[{"name":"stdout","text":"An instance of VectorClass was generated. dim_ = 10\nAn instance of VectorClass was destroyed and 80 bytes freed.\n","output_type":"stream"}],"id":"adcac578-0bca-4aed-8d13-4514826dd0e7"},{"cell_type":"markdown","source":"Our VectorClass is currently pretty useless, since we cannot manipulate the values of the vector entries. Making vec_ public would break encapsulation. Instead let's\n- overload the [] for providing one-based access\n- add a getter method for the dimension\n- add a member function to pretty-print the vector's entries","metadata":{},"id":"1a74105b-5453-49ab-aeed-e0371809fc52"},{"cell_type":"code","source":"#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ){\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n // pretty print vector to given output stream\n void print( std::ostream &os ) {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};","metadata":{},"execution_count":3,"outputs":[],"id":"a32eb08a-822a-4b83-b3d0-6de281acffba"},{"cell_type":"code","source":"// Let's test our extended class\n\nint main() {\n \n VectorClass myVec( 3 );\n \n myVec[ 1 ] = 3;\n myVec[ 2 ] = 2;\n myVec[ 3 ] = 1;\n \n myVec.print( std::cout );\n}\n\nmain();","metadata":{},"execution_count":4,"outputs":[{"name":"stdout","text":"An instance of VectorClass was generated. dim_ = 3\n(3, 2, 1)\nAn instance of VectorClass was destroyed and 24 bytes freed.\n","output_type":"stream"}],"id":"4eb50e73-21f5-4669-9464-32f11d958732"},{"cell_type":"markdown","source":"**What is left on our todo-list?**\nImplement simple methods, such as\n - scaling vector with a constant\n - adding two vectors together\n - compute the Euclidean inner product of two vectors","metadata":{},"id":"d8af8c97-2855-41f9-bda2-9d5ae341b7c0"},{"cell_type":"code","source":"%%file VectorClass.cpp\n\n#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ){\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n // pretty print vector to given output stream (default will be std::cout)\n void print( std::ostream &os = std::cout ) {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n // scale vector with a constant\n void scale( double factor ) {\n // leave that to students\n }\n \n // add to vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n // for the moment leave it with adding another vector to the one on which the method is called.\n \n // let us try the following, and determine, why it will fail ;-)\n void add( VectorClass other ) {\n\n // make sure that input vector has correct length\n assert( other.getDim() == dim_ );\n // for( unsigned int k = 0; k < dim_; k++ ) {\n // vec_[k] += other[k+1];\n // }\n for( unsigned int k = 1; k <= dim_; k++ ) {\n this->operator[](k) += other[k];\n }\n }\n \n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};\n\n// Modify driver a little bit\nint main() {\n\n const unsigned int dim = 10;\n \n // set up 1st vector\n VectorClass myVec( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n }\n myVec.print( std::cout );\n \n // set up 2nd vector\n VectorClass other( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n other[ idx ] = static_cast<double>( idx );\n }\n other.print( std::cout );\n \n // add the 2nd to the 1st\n myVec.add( other );\n myVec.print();\n}","metadata":{},"execution_count":2,"outputs":[{"name":"stdout","text":"Overwriting VectorClass.cpp\n","output_type":"stream"}],"id":"cdfde2f9-fafc-4a9f-99b7-4952720e4521"},{"cell_type":"code","source":"!g++ -g VectorClass.cpp","metadata":{},"execution_count":3,"outputs":[],"id":"f9331dc3-19ff-44a2-a8f5-6e258f9266d2"},{"cell_type":"code","source":"!./a.out","metadata":{},"execution_count":4,"outputs":[{"name":"stdout","text":"An instance of VectorClass was generated. dim_ = 10\n(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)\nAn instance of VectorClass was generated. dim_ = 10\n(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\nAn instance of VectorClass was destroyed and 80 bytes freed.\n(11, 11, 11, 11, 11, 11, 11, 11, 11, 11)\nfree(): double free detected in tcache 2\nAborted\n","output_type":"stream"}],"id":"4f4d196d-761d-40e1-9d08-5df87307476b"},{"cell_type":"markdown","source":"**Note**: The adding itself seems to have worked correctly, but somehow we still have a problem. So what's wrong here?","metadata":{},"id":"3e04234e-4f33-44c4-9adb-0bde893019df"},{"cell_type":"markdown","source":"The answer to this is multifaceted:\n- Our implementation of *add()* used **call-by-copy**\n- Since we pass an object, its **copy constructor** is invoked\n- We haven't implemented one, but the compiler did automatically (thank's for that ;-)\n- Thus, the copy we get of **other** is a **flat** one!","metadata":{},"id":"7551ddc3-e449-47f1-bdc8-9a8080acdaf9"},{"cell_type":"markdown","source":"***\nWe can check on that by making the destructor tell us which block it is going to deallocate and run our code through valgrind:\n***\n**==> valgrind ./a.out**\n<div>\n==28488== Memcheck, a memory error detector</br>\n==28488== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.</br>\n==28488== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info</br>\n==28488== Command: ./a.out</br>\n==28488== </br>\nAn instance of VectorClass was generated. dim_ = 10</br>\n(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)</br>\nAn instance of VectorClass was generated. dim_ = 10</br>\n(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)</br>\n<font color=\"red\">Going to delete memory starting at address 0x4d9b150</font></br>\nAn instance of VectorClass was destroyed and 80 bytes freed.</br>\n(11, 11, 11, 11, 11, 11, 11, 11, 11, 11)</br>\n<font color=\"red\">Going to delete memory starting at address 0x4d9b150</font></br>\n==28488== Invalid free() / delete / delete[] / realloc()</br>\n==28488== at 0x483758B: operator delete[](void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)</br>\n==28488== by 0x109500: VectorClass::~VectorClass() (VectorClass3.cpp:30)</br>\n==28488== by 0x109341: main (VectorClass3.cpp:93)</br>\n==28488== Address 0x4d9b150 is 0 bytes inside a block of size 80 free'd</br>\n==28488== at 0x483758B: operator delete[](void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)</br>\n==28488== by 0x109500: VectorClass::~VectorClass() (VectorClass3.cpp:30)</br>\n==28488== by 0x109322: main (VectorClass3.cpp:100)</br>\n==28488== Block was alloc'd at</br>\n==28488== at 0x483650F: operator new[](unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)</br>\n==28488== by 0x109450: VectorClass::VectorClass(unsigned int) (VectorClass3.cpp:15)</br>\n==28488== by 0x109291: main (VectorClass3.cpp:93)</br>\n==28488== </br>\nAn instance of VectorClass was destroyed and 80 bytes freed.</br>\nGoing to delete memory starting at address 0x4d9ac80</br>\nAn instance of VectorClass was destroyed and 80 bytes freed.</br>\n==28488== </br>\n==28488== HEAP SUMMARY:</br>\n==28488== in use at exit: 0 bytes in 0 blocks</br>\n==28488== total heap usage: 4 allocs, 5 frees, 73,888 bytes allocated</br>\n==28488== </br>\n==28488== All heap blocks were freed -- no leaks are possible</br>\n==28488== </br>\n==28488== For counts of detected and suppressed errors, rerun with: -v</br>\n==28488== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)</br>\n</div>","metadata":{},"id":"02714f7e-52c4-4890-8a87-05ca91d651e5"},{"cell_type":"markdown","source":"Another way to check on this is to remove the automatic copy constructor","metadata":{},"id":"48d75c49-dd79-420e-9e00-c75865245b5c"},{"cell_type":"code","source":"%%file VectorClass.cpp\n\n#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n\n // Don't allow the following default copy constructor either\n VectorClass( VectorClass &other ) = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ){\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n // pretty print vector to given output stream (default will be std::cout)\n void print( std::ostream &os = std::cout ) {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n // scale vector with a constant\n void scale( double factor ) {\n // leave that to students\n }\n \n // add to vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n // for the moment leave it with adding another vector to the one on which the method is called.\n \n // let us try the following, and determine, why it will fail ;-)\n void add( VectorClass other ) {\n\n // make sure that input vector has correct length\n assert( other.getDim() == dim_ );\n // for( unsigned int k = 0; k < dim_; k++ ) {\n // vec_[k] += other[k+1];\n // }\n for( unsigned int k = 1; k <= dim_; k++ ) {\n this->operator[](k) += other[k];\n }\n }\n \n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};\n\n// Modify driver a little bit\nint main() {\n\n const unsigned int dim = 10;\n \n // set up 1st vector\n VectorClass myVec( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n }\n myVec.print( std::cout );\n \n // set up 2nd vector\n VectorClass other( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n other[ idx ] = static_cast<double>( idx );\n }\n other.print( std::cout );\n \n // add the 2nd to the 1st\n myVec.add( other );\n myVec.print();\n}","metadata":{},"execution_count":1,"outputs":[{"name":"stdout","text":"Overwriting VectorClass.cpp\n","output_type":"stream"}],"id":"def80c0f-89b6-4e00-995a-14b8e800975c"},{"cell_type":"code","source":"!g++ VectorClass.cpp","metadata":{},"execution_count":2,"outputs":[{"name":"stdout","text":"VectorClass.cpp: In function ‘int main()’:\nVectorClass.cpp:101:22: error: use of deleted function ‘VectorClass::VectorClass(VectorClass&)’\n myVec.add( other );\n ^\nVectorClass.cpp:26:9: note: declared here\n VectorClass( VectorClass &other ) = delete;\n ^~~~~~~~~~~\nVectorClass.cpp:63:14: note: initializing argument 1 of ‘void VectorClass::add(VectorClass)’\n void add( VectorClass other ) {\n ^~~\n","output_type":"stream"}],"id":"473de9eb-b04c-4087-8444-8d4b3c4c9fb8"},{"cell_type":"markdown","source":"#### Solution?\nHow should we resolve the issue?\n***\nFor the current case the best solution is to change the interface of our `add()` method to be\n\n`void add( const VectorClass& other )`\n\nThe `const` is no must, but makes it clear to other programmers and the compiler that we are not going to change the object\nother inside add().","metadata":{},"id":"91724843-2e97-4862-888a-291fda71444a"},{"cell_type":"code","source":"#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n\n // Don't allow the following default copy constructor either\n VectorClass( VectorClass &other ) = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ){\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n // pretty print vector to given output stream (default will be std::cout)\n void print( std::ostream &os = std::cout ) {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n // scale vector with a constant\n void scale( double factor ) {\n // leave that to students\n }\n \n // add to vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n // for the moment leave it with adding another vector to the one on which the method is called.\n \n // let us try the following, and determine, why it will fail ;-)\n void add( const VectorClass& other ) {\n\n // make sure that input vector has correct length\n assert( other.getDim() == dim_ );\n // for( unsigned int k = 0; k < dim_; k++ ) {\n // vec_[k] += other[k+1];\n // }\n for( unsigned int k = 1; k <= dim_; k++ ) {\n this->operator[](k) += other[k];\n }\n }\n \n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};\n\n// Modify driver a little bit\nint main() {\n\n const unsigned int dim = 10;\n \n // set up 1st vector\n VectorClass myVec( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n }\n myVec.print( std::cout );\n \n // set up 2nd vector\n VectorClass other( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n other[ idx ] = static_cast<double>( idx );\n }\n other.print( std::cout );\n \n // add the 2nd to the 1st\n myVec.add( other );\n myVec.print();\n}","metadata":{},"execution_count":4,"outputs":[{"name":"stderr","text":"input_line_9:61:45: error: no viable overloaded operator[] for type 'const VectorClass'\n this->operator[](k) += other[k];\n ~~~~~^~\ninput_line_9:32:17: note: candidate function not viable: 'this' argument has type 'const VectorClass', but method is not marked const\n double& operator[] ( unsigned int index ){\n ^\ninput_line_9:71:13: error: function definition is not allowed here\n int main() {\n ^\n","output_type":"stream"},{"ename":"Interpreter Error","evalue":"","traceback":["Interpreter Error: "],"output_type":"error"}],"id":"30eccd22-8c5e-4062-a1d1-8006981aae99"},{"cell_type":"markdown","source":"***\nAh, okay. So the `const` was a good idea, but requires a little bit of extra work!\n\nWe need to implement a second version of the operator overloading that returns a const reference!","metadata":{},"id":"a6bfec6d-9eba-4e34-9ec8-e74bfa485664"},{"cell_type":"code","source":"#include <iostream>\n#include <cassert>\n\nclass VectorClass{\n \n public:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n }\n\n // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n\n // Don't allow the following default copy constructor either\n VectorClass( VectorClass &other ) = delete;\n \n // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n // memory leaks\n ~VectorClass() {\n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ){\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n \n // overload the [] for accessing individual entries\n const double& operator[] ( unsigned int index ) {\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n \n // pretty print vector to given output stream (default will be std::cout)\n void print( std::ostream &os = std::cout ) {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n // scale vector with a constant\n void scale( double factor ) {\n // leave that to students\n }\n \n // add to vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n // for the moment leave it with adding another vector to the one on which the method is called.\n \n // let us try the following, and determine, why it will fail ;-)\n void add( const VectorClass& other ) {\n\n // make sure that input vector has correct length\n assert( other.getDim() == dim_ );\n // for( unsigned int k = 0; k < dim_; k++ ) {\n // vec_[k] += other[k+1];\n // }\n for( unsigned int k = 1; k <= dim_; k++ ) {\n this->operator[](k) += other[k];\n }\n }\n \n private:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};\n\n// Modify driver a little bit\nint main() {\n\n const unsigned int dim = 10;\n \n // set up 1st vector\n VectorClass myVec( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n }\n myVec.print( std::cout );\n \n // set up 2nd vector\n VectorClass other( dim );\n for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n other[ idx ] = static_cast<double>( idx );\n }\n other.print( std::cout );\n \n // add the 2nd to the 1st\n myVec.add( other );\n myVec.print();\n}","metadata":{},"execution_count":2,"outputs":[{"name":"stderr","text":"input_line_10:38:23: error: functions that differ only in their return type cannot be overloaded\n const double& operator[] ( unsigned int index ) {\n ~~~~~~~ ^\ninput_line_10:32:17: note: previous definition is here\n double& operator[] ( unsigned int index ){\n ~~~~~~~ ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, char const (&)[50], void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:14:23: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = char [50]]\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, const unsigned int &, void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:14:78: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = unsigned int]\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n ^\ninput_line_10:14:78: error: use of overloaded operator '<<' is ambiguous (with operand types 'basic_ostream<char, std::char_traits<char> >' and 'unsigned int')\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, char const (&)[46], void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:25:23: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = char [46]]\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n ^\ninput_line_10:25:74: error: use of overloaded operator '<<' is ambiguous (with operand types 'basic_ostream<char, std::char_traits<char> >' and 'unsigned long')\n std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, char const (&)[2], void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:45:16: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = char [2]]\n os << \"(\";\n ^\nIn file included from input_line_1:1:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/new:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/exception:144:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/nested_exception.h:40:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/move.h:55:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/type_traits:137:31: error: no member named 'value' in 'std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >'\n : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type\n ~~~~~^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:685:24: note: in instantiation of template class 'std::__and_<std::__not_<std::is_lvalue_reference<std::basic_ostream<char> &> >, std::__is_convertible_to_basic_ostream<std::basic_ostream<char> &>, std::__is_insertable<std::basic_ostream<char> &, const double &, void> >' requested here\n typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,\n ^\ninput_line_10:47:20: note: while substituting deduced template arguments into function template 'operator<<' [with _Ostream = std::basic_ostream<char> &, _Tp = double]\n os << vec_[k] << \", \";\n ^\ninput_line_10:47:20: error: use of overloaded operator '<<' is ambiguous (with operand types 'std::ostream' (aka 'basic_ostream<char>') and 'double')\n os << vec_[k] << \", \";\n ~~ ^ ~~~~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\ninput_line_10:49:16: error: use of overloaded operator '<<' is ambiguous (with operand types 'std::ostream' (aka 'basic_ostream<char>') and 'double')\n os << vec_[dim_-1] << \")\" << std::endl;\n ~~ ^ ~~~~~~~~~~~~\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function\n operator<<(long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function\n operator<<(bool __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function\n operator<<(short __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function\n operator<<(unsigned short __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function\n operator<<(int __n);\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function\n operator<<(long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function\n operator<<(unsigned long long __n)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function\n operator<<(float __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function\n operator<<(long double __f)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]\n operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n ^\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function [with _Traits = std::char_traits<char>]\n operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n ^\ninput_line_10:68:45: error: no viable overloaded operator[] for type 'const VectorClass'\n this->operator[](k) += other[k];\n ~~~~~^~\ninput_line_10:32:17: note: candidate function not viable: 'this' argument has type 'const VectorClass', but method is not marked const\n double& operator[] ( unsigned int index ){\n ^\ninput_line_10:78:13: error: function definition is not allowed here\n int main() {\n ^\nIn file included from input_line_5:1:\nIn file included from /srv/conda/envs/notebook/include/xeus/xinterpreter.hpp:17:\nIn file included from /srv/conda/envs/notebook/include/xeus/xcomm.hpp:19:\nIn file included from /srv/conda/envs/notebook/include/nlohmann/json.hpp:42:\nIn file included from /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/iterator:64:\n/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:568:8: error: no member named 'setstate' in 'std::basic_ostream<char>'\n __out.setstate(ios_base::badbit);\n ~~~~~ ^\ninput_line_10:14:23: note: in instantiation of function template specialization 'std::operator<<<std::char_traits<char> >' requested here\n std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n ^\n","output_type":"stream"},{"ename":"Interpreter Error","evalue":"","traceback":["Interpreter Error: "],"output_type":"error"}],"id":"15b02a70-a496-4098-a446-9a60ee30b116"},{"cell_type":"markdown","source":"Shoot, we have a problem with the **signature** now!\n\nBut, fortunately, there is a way out of this:\n- We can tell the compiler that a member function does not alter its object! This will also be part of the signature. So we can implement our overloading as\n`const double& operator[] ( unsigned int index ) const`\n- We might want to do this also for any other member function that does not change the object, like e.g. `print()`\n\nIf we do this, we end up with the final version:","metadata":{},"id":"6a78d290-77dc-43a5-93f5-15fa2c650ddb"},{"cell_type":"code","source":"#include <iostream>\n#include <cassert>\n\nclass VectorClass {\n \npublic:\n \n // User should specify dimension at object creation\n VectorClass( unsigned int dim ) : dim_(dim) {\n \n // don't allow vectors with zero dimension\n assert( dim_ > 0 );\n\n // allocate memory (will throw an exception, if it fails)\n vec_ = new double[ dim_ ];\n \n // be talkative ;-)\n std::cout << \"An instance of VectorClass was generated. dim_ = \" \n << dim_ << std::endl;\n }\n\n // Don't allow the default constructor\n // [prior to C++11 you would solve this by making it private]\n VectorClass() = delete;\n\n // Don't allow the following default copy constructor either \n VectorClass( VectorClass &other ) = delete;\n \n // We need to implement a destructor to free the dynamic memory again,\n // otherwise we easily produce memory leaks\n ~VectorClass() {\n std::cout << \"Going to delete memory starting at address \"\n << vec_ << std::endl;\n \n delete[] vec_;\n std::cout << \"An instance of VectorClass was destroyed and \"\n << dim_ * sizeof(double)\n << \" bytes freed.\" << std::endl;\n }\n\n // provide access to the vector's dimension\n unsigned int getDim() const { return dim_; }\n \n // overload the [] for accessing individual entries\n double& operator[] ( unsigned int index ) {\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n const double& operator[] ( unsigned int index ) const {\n assert( index != 0 && index <= dim_ );\n return vec_[index-1];\n }\n\n // pretty print vector to given output stream (default will be std::cout)\n void print( std::ostream &os = std::cout ) const {\n os << \"(\";\n for( unsigned int k = 0; k < dim_-1; k++ ) {\n os << vec_[k] << \", \";\n }\n os << vec_[dim_-1] << \")\" << std::endl;\n }\n \n // scale vector with a constant\n void scale( double factor ) {\n // leave that to students\n }\n \n // add to vectors together (that's actually a little bit tricky,\n // due to the question \"where to put the result?\"\n // for the moment leave it with adding another vector to the one on which\n // the method is called.\n void add( const VectorClass& other ) {\n\n // make sure that input vector has correct length\n assert( other.getDim() == dim_ );\n // for( unsigned int k = 0; k < dim_; k++ ) {\n // vec_[k] += other[k+1];\n // }\n for( unsigned int k = 1; k <= dim_; k++ ) {\n this->operator[](k) += other[k];\n }\n }\n \nprivate:\n unsigned int dim_; // dimension of vector\n double* vec_; // entries of vector\n\n};","metadata":{"trusted":true},"execution_count":4,"outputs":[],"id":"0b2a852d-2a07-4f2a-85d2-7833051474d7"},{"cell_type":"code","source":"main();","metadata":{"trusted":true},"execution_count":5,"outputs":[{"name":"stdout","text":"An instance of VectorClass was generated. dim_ = 10\n(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)\nAn instance of VectorClass was generated. dim_ = 10\n(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n(11, 11, 11, 11, 11, 11, 11, 11, 11, 11)\nGoing to delete memory starting at address 0x55e968b9fd90\nAn instance of VectorClass was destroyed and 80 bytes freed.\nGoing to delete memory starting at address 0x55e96870bb20\nAn instance of VectorClass was destroyed and 80 bytes freed.\n","output_type":"stream"}],"id":"77177a38-424a-4558-8836-9c6f95bdd7c0"},{"cell_type":"markdown","source":"***\n#### Separating Implementation and Interface\n\n- Assume that we have the definition of a `class A` in one source file and want to use the class in another source file `B.cpp`.\n- For this the compiler needs to known at least that `class A` exists: \n This can be accomplished by a **forward declaration** of the form `class A;`\n- However, if we also want to access data or function members of A, the compiler needs further info.\n- There are three possibilities:\n 1. Put the complete definition of A into a header file `A.hpp` and include that into the source file `B.cpp`\n 1. Split up the declaration of the member functions and their implementation into two parts. A file `A.hpp` with the class declaration and a file `A.cpp` with the implementation of the member functions.\n 1. Use a combination of both, e.g. to allow inlining of certain short methods.\n- Each of the approaches has pros and cons. So the decision depends on the concrete scenario.\n***\nBrief example on the splitting approach:","metadata":{},"id":"56fb8027-e846-451f-bcda-c28c6e805530"},{"cell_type":"code","source":"%%file splitA.hpp\n\n#include <string>\n\nclass A {\n\n std::string name_;\n\n public:\n void setName( const char* name );\n void printName();\n\n};","metadata":{"trusted":true},"execution_count":1,"outputs":[{"name":"stdout","text":"Writing splitA.hpp\n","output_type":"stream"}],"id":"867de208-5492-44c0-bf04-7ac9f868908f"},{"cell_type":"code","source":"%%file splitA.cpp\n\n// Necessary, because otherwise compiler does not know a class A and a member function setName exists\n#include \"splitA.hpp\"\n\n// Need to prefix the member function name with the class name\nvoid A::setName( const char* name ) {\n name_ = name;\n}","metadata":{"trusted":true},"execution_count":3,"outputs":[{"name":"stdout","text":"Overwriting splitA.cpp\n","output_type":"stream"}],"id":"639efb26-6150-4cdf-aef4-b962ba86a6a3"},{"cell_type":"code","source":"%%file splitB.cpp\n\n#include \"splitA.hpp\"\n\nint main( void ) {\n\n A obj;\n obj.setName( \"An object\" );\n\n // Note: We have not implemented A::printName();\n // However, if it is not used, that's not an issue.\n // obj.printName();\n}","metadata":{"trusted":true},"execution_count":4,"outputs":[{"name":"stdout","text":"Writing splitB.cpp\n","output_type":"stream"}],"id":"3ed47b3b-3b45-4fc3-ab31-655915b41251"},{"cell_type":"code","source":"!g++ splitA.cpp splitB.cpp\n./a.out","metadata":{"trusted":true},"execution_count":6,"outputs":[],"id":"2c897fb0-b3a2-4617-8b92-79dbaaff6b8b"},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[],"id":"782da53e-ca04-42d3-a318-a1501b17b654"}]} \ No newline at end of file