#Circular reference of classes

24 messages · Page 1 of 1 (latest)

pale harbor
#

So here's a toy example of what I want:

#include <iostream>
#include <vector>

class Boss;

class Worker{
  Boss& boss; // using a reference here
  int salary;
public:
  Worker(Boss& b, int s){
    this->boss = b;
    this->salary = s;
  }
  // other logic, whatever
};

class Boss{
  std::vector<Worker> workers;
public:
  // constructors, etc
  Boss(){}
  Worker hireSomebody(){
    Worker w = Worker(*this, 25);
    // I've hard coded "25" here, but assume that I do some calculations and whatnot to obtain that value.
    return w;
  }

};



int main() {
    // Write C++ code here
    std::cout << "Hello world!";

    return 0;
}

I know that I can probably do it some other way (by creating a getHired() function from a worker, or something similar), but How do I do it this way?
My only requirement is that the "owner" object is "constructing" a "child" object.

ofcourse, when I try to compile this, I get something similar to invalid use of incomplete type ‘class Worker’ and return type ‘class Worker’ is incomplete

elder oakBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

mellow pike
#

move Boss below Worker

#

And then forward declare Boss before Worker

nimble heart
#

You'll need to forward declare classes and also possibly define members separately from the class body

mellow pike
#

so:

class Boss;

class Worker {
  // body
};

class Boss {
  // body
};
pale harbor
#

i see

#

ok so here is my example code again

#include <iostream>
#include <vector>

class Boss;

class Worker{
  Boss& boss; // using a reference here
  int salary;
public:
  Worker(Boss& b, int s){
    this->boss = b;
    this->salary = s;
  }
  // other logic, whatever
};

class Boss{
  std::vector<Worker> workers;
public:
  // constructors, etc
  Boss(){}
  Worker hireSomebody(){
    Worker w = Worker(*this, 25);
    // I've hard coded "25" here, but assume that I do some calculations and whatnot to obtain that value.
    return w;
  }

};



int main() {
    // Write C++ code here
    std::cout << "Hello world!";

    return 0;
}
#

I still get the same error

#
/tmp/xKnfUrMlJn.cpp: In constructor 'Worker::Worker(Boss&, int)':
/tmp/xKnfUrMlJn.cpp:11:3: error: uninitialized reference member in 'class Boss&' [-fpermissive]
   11 |   Worker(Boss& b, int s){
      |   ^~~~~~
/tmp/xKnfUrMlJn.cpp:8:9: note: 'Boss& Worker::boss' should be initialized
    8 |   Boss& boss; // using a reference here
      |         ^~~~
/tmp/xKnfUrMlJn.cpp:12:11: error: invalid use of incomplete type 'class Boss'
   12 |     this->boss = b;
      |     ~~~~~~^~~~
/tmp/xKnfUrMlJn.cpp:5:7: note: forward declaration of 'class Boss'
    5 | class Boss;
      |       ^~~~
nimble heart
#

You can't use Boss before it's defined

#

You can have a reference or pointer to Boss, but you can't do anything with it

#

this->boss = b; is invoking the copy constructor and is also improper initialization of a reference

pale harbor
#

oh

nimble heart
#

Write you constructor as Worker(Boss& b, int s) : b(b), s(s) {}

pale harbor
#

i thought they worked like pointers

nimble heart
#

They have a lot in common with pointers, just some syntactic differences

pale harbor
#

ok imma try this out too

#

oh hey it actually worked

#

thanks man

elder oakBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

nimble heart
#

Excellent