Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to pass a <code>Job</code> pointer to the <code>Display</code>, and vice versa, so they know about each other, eg:</p> <p>Main:</p> <pre><code>#include "Display.h" #include "Job.h" int main() { Display display; Job job; display.init(&amp;job); job.init(&amp;display); display.test(); return 0; } </code></pre> <p>Display.h:</p> <pre><code>class Job; class Display { private: Job *_job; public: Display(); void init(Job *job); void test(); void test2(const std::string &amp;ttt); }; </code></pre> <p>Display.cpp:</p> <pre><code>#include "Display.h" Display::Display() : _job(NULL) { } void Display::init(Job *job) { _job = job; } void Display::test() { std::cout &lt;&lt; "testing.." &lt;&lt; std::endl; } void Display::test2(const std::string &amp;ttt) { std::cout &lt;&lt; "testing3333...." &lt;&lt; _job-&gt;getThree() &lt;&lt; std::endl; std::cout &lt;&lt; "testing2222...." &lt;&lt; ttt &lt;&lt; std::endl; } </code></pre> <p>Job.h:</p> <pre><code>class Display; class Job { private: Display *_display; public: Job(); void init(Display *display); std::string getThree(); }; </code></pre> <p>Job.cpp:</p> <pre><code>#include "Job.h" Job::Job() : _display(NULL) { } void Job::init(Display *display) { _display = display; _display-&gt;test2("from Job"); } std::string Job::getThree() { return "test3"; } </code></pre> <p>Given the requirements you mentioned, the <code>Display</code> does not really need to remember the <code>Job</code>, only the <code>Job</code> needs to remember the <code>Display</code>, so you could do something like this as an alternative:</p> <p>Main:</p> <pre><code>#include "Display.h" #include "Job.h" int main() { Display display; Job job; job.init(&amp;display); display.test(); return 0; } </code></pre> <p>Display.h:</p> <pre><code>class Job; class Display { public: void test(); void test2(Job *job); }; </code></pre> <p>Display.cpp:</p> <pre><code>#include "Display.h" void Display::test() { std::cout &lt;&lt; "testing.." &lt;&lt; std::endl; } void Display::test2(Job *job) { std::cout &lt;&lt; "testing3333...." &lt;&lt; job-&gt;getThree() &lt;&lt; std::endl; } </code></pre> <p>Job.h:</p> <pre><code>class Display; class Job { private: Display *_display; public: Job(); void init(Display *display); std::string getThree(); }; </code></pre> <p>Job.cpp:</p> <pre><code>#include "Job.h" Job::Job() : _display(NULL) { } void Job::init(Display *display) { _display = display; _display-&gt;test2(this); } std::string Job::getThree() { return "test3"; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload