I'm in desperate need of help and direction. Been trying to get this to compile, but battling due to the fact that there are 3 classes and not hundreds on how the includes/forward declarations should work here.
The error is marked in person.h.
In addition to the error, there is a warning I've marked in main.cpp.
This is a console app.
Thank you in advance.
person.h
#ifndef PERSON_H
#define PERSON_H
#include <QString>
class Employer;
class Person
{
public:
Person(QString name);
QString toString() const;
void setPosition(Employer newE, Position newP);
Position getPosition() const;
private:
QString m_Name;
bool m_Employed;
Position m_Position;
Employer m_Employer; //--> ERROR: field `m_Employer' has incomplete type.
};
#endif // PERSON_H
person.cpp
#include "employer.h"
#include "position.h"
#include "person.h"
Person::Person(QString name) : m_Name(name), m_Employed(false), m_Position(""), m_Employer("")
{
}
void Person::setPosition(Employer newE, Position newP)
{
m_Position = newP;
m_Employed = true;
m_Employer = newE;
}
Position Person::getPosition()const
{
return (m_Employed ? m_Position : Position("Professional nose-picker"));
}
QString Person::toString()const
{
return m_Name + ", Employed: " + (m_Employed ? "Yes" : "No") + ", Position: " + getPosition().toString();
}
employer.h
#ifndef EMPLOYER_H
#define EMPLOYER_H
#include <QString>
//class Position;
//class Person;
class Employer
{
public:
Employer(QString name, QString market = "");
void hire(Person &newHire, Position pos);
QString toString() const;
private:
QString m_Name;
QString m_Market;
};
#endif // EMPLOYER_H
employer.cpp
#include "employer.h"
#include "person.h"
#include "position.h"
Employer::Employer(QString name, QString market) : m_Name(name), m_Market(market)
{
}
QString Employer::toString()const
{
return m_Name + (m_Market != "" ? ", Market: " + m_Market : "");
}
void Employer::hire(Person &newHire, Position pos)
{
newHire.setPosition(*this, pos);
}
position.h
#ifndef POSITION_H
#define POSITION_H
#include <QString>
class Position
{
public:
Position(QString name, QString desc = "");
QString toString() const;
private:
QString m_Name;
QString m_Desc;
};
#endif // POSITION_H
position.cpp
#include "position.h"
Position::Position(QString name, QString desc)
:m_Name(name), m_Desc(desc)
{}
QString Position::toString()const
{
return m_Name + (m_Desc != "" ? ", Description: " + m_Desc : "");
}
main.cpp
#include <QtCore/QCoreApplication>
#include <QTextStream>
#include "position.h"
#include "person.h" //--WARNING -> In file included from main.cpp:4:
#include "employer.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream cout(stdout);
Person bob("Bob");
Employer devCo("Dev Co");
cout << bob.toString() << endl;
bob.setPosition(is, Position("Developer", "Software Eng"));
cout << bob.toString() << endl;
cout << devCo.toString() << endl;
return a.exec();
}
The Person class needs to know all about Employer and so needs Employer.h included in it rather than the class forward declaration.
In this case the person needs to know the size of the Employee class as you have an Employee stored inside the class.
If you wanted to decouple the classes and just use a forward declaration then Person would have to contain a pointer (raw or smart) to the employer.