从C ++入门班进入循环依赖 [英] Classes from C++ Primer going into Cyclic Dependency

查看:449
本文介绍了从C ++入门班进入循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这本书 - C ++入门学习C ++。我在章介绍类中间,我被困在解决头文件包括作为例子中有两类。

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:

#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, ' ');
}

这是我的项目结构:

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

我使用的 code :: Blocks的的IDE。我已经添加了 / src目录 /包括在编译器文件夹中的搜索目录的设置。我还添加项目根目录到搜索目录。

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?

推荐答案

这很简单:

您的程序#包括ScreenCls.h又包括WindowManager.h。但ScreenCls.h中的WindowManager.h的#包括什么也不做,因为它已经被列入到现在WindowManager.h不知道ScreenCls是什么。

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.

这篇关于从C ++入门班进入循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆