class - Qt 4 C++ Getting an error when using 3 classes that use each other, error: field `m_Employer' has incomplete type -
i'm in desperate need of , direction. been trying compile, battling due fact there 3 classes , not hundreds on how includes/forward declarations should work here.
the error marked in person.h.
in addition error, there warning i've marked in main.cpp.
this console app.
thank 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 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 know employer , needs employer.h included in rather class forward declaration.
in case person needs know size of employee class have employee stored inside class.
if wanted decouple classes , use forward declaration person have contain pointer (raw or smart) employer.
Comments
Post a Comment