0

I am following C++ Primer, and currently trying to understand class. Here is this example inside the book:

class Window_mgr //Window_mgr class definition
{
public:
    using ScreenIndex = std::vector<Screen>::size_type;

    void clear(ScreenIndex);

private:
    std::vector<Screen> screens {Screen(24,80,' ')};
};

class Screen //Screen class definition
{
public:
    friend void Window_mgr::clear(ScreenIndex);

    using pos = std::string::size_type;

/* lots of codes thereafter (from the book) */
};

when I try to 'Build' (on CodeBlocks 20.03 on Win 10), I get the following errors:

I:\C++ Projects and source files\ScribblingRough.cpp|13|error: 'Screen' was not declared in this scope|
I:\C++ Projects and source files\ScribblingRough.cpp|13|note: suggested alternative: '_freea'|
I:\C++ Projects and source files\ScribblingRough.cpp|13|error: template argument 1 is invalid|
I:\C++ Projects and source files\ScribblingRough.cpp|13|error: template argument 2 is invalid|
I:\C++ Projects and source files\ScribblingRough.cpp|15|error: 'ScreenIndex' has not been declared|
I:\C++ Projects and source files\ScribblingRough.cpp|18|error: 'Screen' was not declared in this scope|
I:\C++ Projects and source files\ScribblingRough.cpp|18|note: suggested alternative: '_freea'|
I:\C++ Projects and source files\ScribblingRough.cpp|18|error: template argument 1 is invalid|
I:\C++ Projects and source files\ScribblingRough.cpp|18|error: template argument 2 is invalid|
I:\C++ Projects and source files\ScribblingRough.cpp|18|error: 'Screen' was not declared in this scope|
I:\C++ Projects and source files\ScribblingRough.cpp|18|note: suggested alternative: 'screens'|
I:\C++ Projects and source files\ScribblingRough.cpp|18|error: cannot convert '<brace-enclosed initializer list>' to 'int' in initialization|
I:\C++ Projects and source files\ScribblingRough.cpp|24|error: 'ScreenIndex' has not been declared|
I:\C++ Projects and source files\ScribblingRough.cpp|93|error: variable or field 'clear' declared void|
I:\C++ Projects and source files\ScribblingRough.cpp|93|error: 'ScreenIndex' was not declared in this scope|
I:\C++ Projects and source files\ScribblingRough.cpp|93|note: suggested alternative: 'Screen'|

I have followed the instructions from the book: First defining the Window_mgr class (which declares, but not define clear) Second - define Screen class including a friend declaration for clear Finally define clear.

Please help me understand my errors of understanding. Thanks.

SogaBan
  • 15
  • 5
  • Without having read the book, it's fairly likely that something was misunderstood. Based on the shown code it's clearn that `Screen` must be defined first. – Sam Varshavchik Aug 10 '22 at 00:50
  • If you are going to use `Screen`s in `Window_mgr`, then `Screen` must be placed ahead of `Window_mgr`. – jkb Aug 10 '22 at 00:51
  • 3
    Odd... here's [another SO question](https://stackoverflow.com/questions/31342839/member-function-a-friend) that claims to be quoting C++ Primer, but with the code reordered. And still not compiling, due to the code order. And [here's another](https://stackoverflow.com/questions/69269132/should-i-put-these-classes-in-their-own-header-files-if-so-how), again with the code in a new order. I do not own this book, so I can not unravel this mystery. Perhaps the book keeps revising this code until it is correct? – Drew Dormann Aug 10 '22 at 01:24

0 Answers0