build - Classes from C++ Primer going into Cyclic Dependency -
i following book - c++ primer learning c++. i'm in middle of chapter introducing classes, , i'm stuck in resolving header files include of 2 classes taken example there.
here 2 classes, , header files:
screencls.h:
#ifndef screencls_h #define screencls_h #include <iostream> #include "windowmanager.h" using namespace std; class screencls { friend void windowmanager::clear(screenindex); public: typedef string::size_type pos; screencls() { } screencls(pos height, pos width, char c): height(height), width(width), contents(height * width, c) { } screencls &set(char); screencls &set(pos, pos, char); char get() const { return contents[cursor]; } // implicitly inline private: pos cursor; pos height; pos width; string contents; }; #endif // screencls_h
screencls.cpp:
#include "screencls.h" char screencls::get(pos r, pos c) const { pos row = r * width; return contents[row + c]; } screencls &screencls::set(char ch) { contents[cursor] = ch; return *this; } screencls &screencls::set(pos r, pos c, char ch) { contents[r * width + c] = ch; return *this; }
windowmanager.h:
#ifndef windowmanager_h #define windowmanager_h #include <iostream> #include <vector> #include "screencls.h" using namespace std; class windowmanager { public: // location id each screen on window using screenindex = vector<screencls>::size_type; // reset screen @ given position blanks void clear(screenindex); private: vector<screencls> screens{ screen(24, 80, ' ') }; }; #endif // windowmanager_h
windowmanager.cpp:
#include "windowmanager.h" #include "screencls.h" void windowmanager::clear(screenindex index) { screencls &s = screens[i]; s.contents = string(s.height * s.width, ' '); }
this project structure:
/src/screencls.cpp /src/windowmanager.cpp /include/screencls.h /include/windowmanager.h
i'm using code::blocks ide. i've added /src
, /include
folder in search directory in compiler settings. i've added project root directory search directory.
now, when i'm trying build project, shows following errors:
'screencls' not declared in scope (windowmanager.h) 'screenindex' has not been declared (windowmanager.h) 'screenindex' has not been declared (screencls.h)
and i've no idea what's happening here. searched through resources online, , found this link. , it's not helping here. can take time , suggest solution?
it's simple:
your program #includes "screencls.h" in turn includes "windowmanager.h". #include of "screencls.h" in "windowmanager.h" nothing because has been included , windowmanager.h not know screencls is.
you need forward declaration instead means either declare of class necessary or use pointers.
Comments
Post a Comment