{"id":368,"date":"2015-04-29T18:30:26","date_gmt":"2015-04-29T17:30:26","guid":{"rendered":"http:\/\/www.pixnbgames.com\/blog\/?p=368"},"modified":"2015-05-16T09:21:50","modified_gmt":"2015-05-16T08:21:50","slug":"cross-references-in-cpp-classes","status":"publish","type":"post","link":"https:\/\/www.pixnbgames.com\/blog\/c\/cross-references-in-cpp-classes\/","title":{"rendered":"Cross references in C++ classes"},"content":{"rendered":"<h1>What are cross references?<\/h1>\n<p style=\"text-align: justify;\">It&#8217;s named cross references the situation in which a class A needs to reference to class B, and class B also needs to make reference to class A.<\/p>\n<p style=\"text-align: justify;\">For instance, suppose we have an application responsible of the management of a car workshop, where each mechanic is repairing an only car at every moment. We need references from the mechanic to the car, and from the car to the mechanic. We can have something like this:<\/p>\n<h2 style=\"text-align: justify;\"><em>Mechanic.h<\/em><\/h2>\n<pre lang=\"Cpp\" line=\"1\">#ifndef _MECHANIC_H_\r\n#define _MECHANIC_H_\r\n\r\n#include \"Car.h\"\r\n\r\nclass Mechanic {\r\npublic:\r\n    void repairCar(Car* car);\r\nprivate:\r\n    Car* _car;\r\n}\r\n\r\n#endif \/\/ _MECHANIC_H_\r\n<\/pre>\n<h2 style=\"text-align: justify;\"><em>Car.h<\/em><\/h2>\n<pre lang=\"Cpp\" line=\"1\">#ifndef _CAR_H_\r\n#define _CAR_H_\r\n\r\n#include \"Mechanic.h\"\r\n\r\nclass Car {\r\npublic:\r\n    void assignMechanic(Mechanic* mechanic);\r\nprivate:\r\n    Mechanic* _mechanic;\r\n}\r\n\r\n#endif \/\/ _CAR_H_\r\n<\/pre>\n<p style=\"text-align: justify;\">If we try to compile the previous code we will get an error, because the compiler tries to compile one of the classes, for example <code>Mechanic<\/code>. Then it sees that for compile the class <code>Mechanic<\/code>, it needs the class <code>Car<\/code>. So then it tries to compile the class <code>Car<\/code> but now it finds that is needed the class  <code>Mechanic<\/code>, which is not compiled yet. As two classes are needed mutually, it&#8217;s not possible to continue for the compiler.<\/p>\n<h2>Solution<\/h2>\n<p style=\"text-align: justify;\">The way we can fix this issue in C++ is very simple. We will have one of these classes exactly as shown before. For example, we leave as earlier the class  <code>Mechanic<\/code>:<\/p>\n<h2 style=\"text-align: justify;\"><em>Mechanic.h<\/em><\/h2>\n<pre lang=\"Cpp\" line=\"1\">#ifndef _MECHANIC_H_\r\n#define _MECHANIC_H_\r\n\r\n#include \"Car.h\"\r\n\r\nclass Mechanic {\r\npublic:\r\n    void repairCar(Car* car);\r\nprivate:\r\n    Car* _car;\r\n}\r\n\r\n#endif \/\/ _MECHANIC_H_\r\n<\/pre>\n<p style=\"text-align: justify;\">However, in the class <code>Coche<\/code> we will make two changes. On the one hand, we need to delete the <code>#include<\/code> of <em>Mechanic.h<\/em>. And on the other hand, we need to declare that exists a class named <code>Mechanic<\/code>, but we don&#8217;t define it.<\/p>\n<h2 style=\"text-align: justify;\"><em>Car.h<\/em><\/h2>\n<pre lang=\"Cpp\" line=\"1\">#ifndef _CAR_H_\r\n#define _CAR_H_\r\n\r\nclass Mechanic;\r\n\r\nclass Car {\r\npublic:\r\n    void assignMechanic(Mechanic* mechanic);\r\nprivate:\r\n    Mechanic* _mechanic;\r\n}\r\n\r\n#endif \/\/ _CAR_H_\r\n<\/pre>\n<p style=\"text-align: justify;\">Now we can compile our program saving the problem with cross references.<\/p>\n<p style=\"text-align: justify;\">Regards!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What are cross references? It&#8217;s named cross references the situation in which a class A needs to reference to class B, and class B also needs to make reference to class A. For instance, suppose we have an application responsible <a class=\"more-link\" href=\"https:\/\/www.pixnbgames.com\/blog\/c\/cross-references-in-cpp-classes\/\">Seguir leyendo <span class=\"screen-reader-text\">  Cross references in C++ classes<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[28],"tags":[31,30],"language":[4],"_links":{"self":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/368"}],"collection":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/comments?post=368"}],"version-history":[{"count":11,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/368\/revisions"}],"predecessor-version":[{"id":379,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/368\/revisions\/379"}],"wp:attachment":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/media?parent=368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/categories?post=368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/tags?post=368"},{"taxonomy":"language","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/language?post=368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}