{"id":2742,"date":"2022-08-30T15:27:24","date_gmt":"2022-08-30T15:27:24","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/08\/errors-implementing-a-dynamic-matrix-class-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:27:24","modified_gmt":"2022-08-30T15:27:24","slug":"errors-implementing-a-dynamic-matrix-class-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/errors-implementing-a-dynamic-matrix-class-collection-of-common-programming-errors\/","title":{"rendered":"Errors implementing a dynamic matrix class-Collection of common programming errors"},"content":{"rendered":"<p>Alright, I&#8217;m trying to implement a simple 2D matrix class right now. This is what it looks like so far:<\/p>\n<pre><code>template \nclass dyMatrix {\n    private:\n        Type *mat;\n\n        int width, height;\n        int length;\n\n    public:\n        dyMatrix (int _width, int _height)\n            : width(_width), height(_height), mat(0)\n        {\n            length = width * height;\n            mat = new Type[length];\n        };\n\n        \/\/ ---\n\n        int getWidth() {\n            return width;\n        };\n\n        int getHeight() {\n            return height;\n        };\n\n        int getLength() {\n            return length;\n        }\n\n        \/\/ ---\n\n        Type&amp; operator() (int i, int j) {\n            return mat[j * width + i];\n        };\n\n        Type&amp; operator() (int i) {\n            return mat[i];\n        };\n\n        \/\/ ---\n\n        ~dyMatrix() {\n            delete[] mat;\n        };\n};\n<\/code><\/pre>\n<p>To test it, and compare with static multi-dimensional arrays, I wrote the following snippet of code:<\/p>\n<pre><code>#include \nusing namespace std;\n\n\/* matrix class goes here *\/\n\nstruct Coord {\n    int x, y;\n\n    Coord()\n        : x(0), y(0)\n    {};\n\n    Coord (int _x, int _y)\n        : x(_x), y(_y)\n    {};\n\n    void print() {\n        cout<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2014-02-08 15:49:24. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Alright, I&#8217;m trying to implement a simple 2D matrix class right now. This is what it looks like so far: template class dyMatrix { private: Type *mat; int width, height; int length; public: dyMatrix (int _width, int _height) : width(_width), height(_height), mat(0) { length = width * height; mat = new Type[length]; }; \/\/ &#8212; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2742","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2742"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2742\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}