Classes from C++ Primer going into Cyclic Dependency

一曲冷凌霜 提交于 2019-12-12 00:27:05

问题


I am following the book - C++ Primer for learning C++. I'm in middle of the chapter introducing classes, and I'm stuck in resolving the header files include of two classes taken as example there.

Here are the two classes, and the 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 for each screen on window
    using ScreenIndex = vector<ScreenCls>::size_type;
    // reset the Screen at the given position to all 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 is my project structure:

/src/ScreenCls.cpp 
/src/WindowManager.cpp 
/include/ScreenCls.h 
/include/WindowManager.h

I'm using Code::Blocks IDE. I've added /src and /include folder in the Search Directory in the compiler Settings. I've also added the project root directory to the search directory.

Now, when I'm trying to build the project, it shows following errors:

'ScreenCls' was not declared in this 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. I searched through some resources online, and found this link. And it's not helping here. Can someone take some time to look and suggest a solution?


回答1:


It's simple:

Your program #includes "ScreenCls.h" which in turn includes "WindowManager.h". But the #include of "ScreenCls.h" in "WindowManager.h" does nothing because it has already been included and now WindowManager.h does not know what ScreenCls is.

You need a forward declaration instead which means you either declare as much of your class as necessary or use pointers.



来源:https://stackoverflow.com/questions/16547560/classes-from-c-primer-going-into-cyclic-dependency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!