{"id":1336,"date":"2018-07-10T09:58:59","date_gmt":"2018-07-10T09:58:59","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=1336"},"modified":"2024-02-16T10:26:32","modified_gmt":"2024-02-16T10:26:32","slug":"breaking-cyclic-dependencies-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=1336","title":{"rendered":"Cyclic dependencies (C++)"},"content":{"rendered":"<p><strong>Issue<\/strong><br \/>\nThe cyclic dependency is a problem which occurs if two classes includes each other in their header.<\/p>\n<pre class=\"brush: cpp; title: hen.hpp; notranslate\" title=\"hen.hpp\">\r\n#pragma once\r\n#include &quot;egg.hpp&quot;\r\n\r\nclass Hen {\r\n    Egg egg;\r\n};\r\n<\/pre>\n<pre class=\"brush: cpp; title: hen.cpp; notranslate\" title=\"hen.cpp\">\r\n#include &quot;hen.hpp&quot;\r\n\r\n\/\/ ...\r\n<\/pre>\n<pre class=\"brush: cpp; title: egg.hpp; notranslate\" title=\"egg.hpp\">\r\n#pragma once\r\n#include &quot;hen.hpp&quot;\r\n\r\nclass Egg {\r\n    Hen hen;\r\n};\r\n<\/pre>\n<pre class=\"brush: cpp; title: egg.cpp; notranslate\" title=\"egg.cpp\">\r\n#include &quot;egg.hpp&quot;\r\n\r\n\/\/ ...\r\n<\/pre>\n<pre>\r\nhen.hpp <---> egg.hpp\r\n   \u2191             \u2191\r\n   |             |\r\n   |             |\r\nhen.cpp       egg.cpp\r\n<\/pre>\n<hr>\n<p><strong>Solution<\/strong><br \/>\nYou can fix it if you just declare the other class in the header and include them later in the source file.<\/p>\n<pre class=\"brush: cpp; title: hen.hpp; notranslate\" title=\"hen.hpp\">\r\n#pragma once\r\nclass Egg;\r\n\r\nclass Hen {\r\n    Egg egg;\r\n};\r\n<\/pre>\n<pre class=\"brush: cpp; title: hen.cpp; notranslate\" title=\"hen.cpp\">\r\n#include &quot;hen.hpp&quot;\r\n#include &quot;egg.hpp&quot;\r\n\r\n\/\/ ...\r\n<\/pre>\n<pre class=\"brush: cpp; title: egg.hpp; notranslate\" title=\"egg.hpp\">\r\n#pragma once\r\nclass Hen;\r\n\r\nclass Egg {\r\n    Hen hen;\r\n};\r\n<\/pre>\n<pre class=\"brush: cpp; title: egg.cpp; notranslate\" title=\"egg.cpp\">\r\n#include &quot;egg.hpp&quot;\r\n#include &quot;hen.hpp&quot;\r\n\r\n\/\/ ...\r\n<\/pre>\n<pre>\r\nhen.hpp       egg.hpp\r\n   \u2191    \u2196   \u2197    \u2191\r\n   |      \u292c      |\r\n   |    \/   \\    |\r\nhen.cpp       egg.cpp\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Issue The cyclic dependency is a problem which occurs if two classes includes each other in their header. hen.hpp egg.hpp<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[27],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/1336"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1336"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/1336\/revisions"}],"predecessor-version":[{"id":16759,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/1336\/revisions\/16759"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1336"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}