Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7597243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:06:38+00:00 2026-05-30T22:06:38+00:00

So I have one main file, and two classes: A display Class, and a

  • 0

So I have one main file, and two classes: A display Class, and a job class.

What I want to achieve, is for the main class to be able to call the display class, as well as interface with the job class, BUT I also want the Job Class to be able to call methods from the Display class and send parameters to the display class as well.

I have tried multiple ways of fixing my current problem, but I have not been able to accomplish what I am wanting, I have heard about namespaces, but I am unfamiliar with them and am not sure if this is what I need.

I have also tried to pass the Job/Display objects from main, but that has not worked with what I want to do since in my header I already end up defining a new object.

Here is some example code of what I want to achieve (Please ignore simple compiler errors/This is just example code, and I am not going to post my entire project because that would be way to long/Ignore headers):

Main.cpp

 int main(){
    Display display;
    Job job;
    job.init();
    display.test();
    return 0;
}

Display.cpp

 void Display::test(){
     std::cout << "testing.." << std::endl;
 }

 void Display::test2(std::string ttt){
     Job job; //Do not want to create a whole new object here
     std::cout << "testing3333...." << job.getThree() << std::endl;
     std::cout << "testing2222...." <<  ttt << std::endl;
 }

Job.cpp

 void Job::init(){
      Display disp2; //I do not want to create a whole new object here, but I can't fix this
      disp2.test2("from Job");
 }

 std::string Job::getThree(){
       return "test3";
 }

Job.h

class Job{
private:
    Display disp; // Do not want a new object here as well
public:
    void init();
    std::string getThree();
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:06:40+00:00Added an answer on May 30, 2026 at 10:06 pm

    You need to pass a Job pointer to the Display, and vice versa, so they know about each other, eg:

    Main:

    #include "Display.h"
    #include "Job.h"
    
    int main()
    { 
        Display display; 
        Job job; 
        display.init(&job);
        job.init(&display); 
        display.test(); 
        return 0; 
    } 
    

    Display.h:

    class Job;
    
    class Display
    {
    private:
        Job *_job;
    public:
        Display();
        void init(Job *job);
        void test();
        void test2(const std::string &ttt);
    };
    

    Display.cpp:

    #include "Display.h"
    
    Display::Display()
        : _job(NULL)
    {
    }
    
    void Display::init(Job *job)
    {
        _job = job;
    }
    
    void Display::test()
    { 
        std::cout << "testing.." << std::endl; 
    } 
    
    void Display::test2(const std::string &ttt)
    { 
        std::cout << "testing3333...." << _job->getThree() << std::endl; 
        std::cout << "testing2222...." << ttt << std::endl; 
    } 
    

    Job.h:

    class Display;
    
    class Job
    {
    private:
        Display *_display;
    public:
        Job();
        void init(Display *display);
        std::string getThree();
    };
    

    Job.cpp:

    #include "Job.h"
    
    Job::Job()
        : _display(NULL)
    {
    }
    
    void Job::init(Display *display)
    { 
        _display = display;
        _display->test2("from Job"); 
    } 
    
    std::string Job::getThree()
    { 
        return "test3"; 
    } 
    

    Given the requirements you mentioned, the Display does not really need to remember the Job, only the Job needs to remember the Display, so you could do something like this as an alternative:

    Main:

    #include "Display.h"
    #include "Job.h"
    
    int main()
    { 
        Display display; 
        Job job;
        job.init(&display); 
        display.test(); 
        return 0; 
    } 
    

    Display.h:

    class Job;
    
    class Display
    {
    public:
        void test();
        void test2(Job *job);
    };
    

    Display.cpp:

    #include "Display.h"
    
    void Display::test()
    { 
        std::cout << "testing.." << std::endl; 
    } 
    
    void Display::test2(Job *job)
    { 
        std::cout << "testing3333...." << job->getThree() << std::endl; 
    } 
    

    Job.h:

    class Display;
    
    class Job
    {
    private:
        Display *_display;
    public:
        Job();
        void init(Display *display);
        std::string getThree();
    };
    

    Job.cpp:

    #include "Job.h"
    
    Job::Job()
        : _display(NULL)
    {
    }
    
    void Job::init(Display *display)
    { 
        _display = display;
        _display->test2(this); 
    } 
    
    std::string Job::getThree()
    { 
        return "test3"; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written two simple Java classes (one of them containing main(), and the
I will have two files. One header.h file and second one is main.c file.
I have an application that has two threads. The first one (the main thread)
I'm using the jQuery Validation JS I have two contact forms, the main one
I have a jar file. I want to know which external classes and methods
Hello ever one I have made a main function and two files one header
Ok I have two modules, each containing a class, the problem is their classes
I have two threads One and Two. defined by their respective classes in the
I have started a new project consisting of one main.cpp file and an additional
I have two classes under QT, one to make a form, the other to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.