Circular dependency c++ fails with forward decleration -
i have 2 classes, meassurementiteration , meassurementset. meassurementiteration has vector of sets, , set has pointer iteration.
meassurementiteration
#pragma once #include <vector> #include "meassurementsetrepo.h" #include "meassurementset.h" class meassurementiteration { public: meassurementiteration(const int id, const long start, const long end, const bool active) : id_(id) , start_(start) , end_(end) , active_(active) {} meassurementiteration(); ~meassurementiteration() = default; std::vector<meassurementset>& getmeasurementset() { /** magic **/ return this->mssets_; } private: const int id_; const long start_; long end_; bool active_; meassurementsetrepo mssetrepo; std::vector<meassurementset> mssets_; };
meassurementset
#pragma once #include <string> #include <memory> #include "meassurementiterationrepo.h" class meassurementiteration; class meassurementset { public: meassurementset(const int id, const std::string & way, const int it_id) : id_(id) , msway_(way) , meassurementiteration_id_(it_id) { msit_ = nullptr; } ~meassurementset() { delete msit_; } meassurementiteration& getmsiteration() { if (this->msit_ == nullptr) { /** magic sets msit **/ } return *msit_; } private: int id_; const std::string msway_; const int meassurementiteration_id_; meassurementiterationrepo msitrepo_; meassurementiteration * msit_; };
i cannot understand why i'm getting circular dependency here. when read other posts, seems way it. can explain me, i'm doing wrong?
update: problem seems due use of opposite class'es repository. illustrate, see figure.
how can solve dependency-hell?
there 1 important principle learn, , interfacing.
the idea when want use 1 of classes, want know what class does. not need know how it, aka implementation details.
that 1 of reasons why c++ separates files header files , source files. header files should contain does, source files how it's done. 1 argue new format in header file contains nothing public declarations (that is, without private member variables, private helper methods etc.).
using cause. don't see problematic line in particular @ first glance, might iterationrepo includes iteration includes set includes iterationrepo.
how having interfaces you: in headers, declare only, not implement (some one-liners, recommend not doing either). forward-declare only. include in source files. can't have circular dependency way.
(the rule broken member variable types of course - if has member variable b, needs know b in order know it's size, therefore needs include in header.)
Comments
Post a Comment