类未在范围中声明 - 即使包括.h [英] class not declared in scope - even though .h was included

查看:217
本文介绍了类未在范围中声明 - 即使包括.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对c ++比较陌生,我遇到了一个奇怪的问题,

i am relatively new to c++ and I am running into an odd issue,

我得到ToDoItem没有在这个范围内声明,但我已经包括todoitem.h文件

I am getting "ToDoItem was not declared in this scope" yet I have included the todoitem.h file

错误在此类定义中:

#ifndef ITEMMONITOR_H
#define ITEMMONITOR_H

#include <QObject>
#include <QPointer>
#include <QTimer>
#include "todoitem.h"

class ItemMonitor : public QObject{
    Q_OBJECT

    signals:
        void finished();

    public:
        explicit ItemMonitor(std::vector< QPointer<ToDoItem> >&  items_);

    private:
        std::vector< QPointer<ToDoItem> >& items;
        bool shouldRun;

    public slots:
        void beginMonitoring();
        void finishUp();
};

#endif // ITEMMONITOR_H

且todoitem.h为:

and the todoitem.h is:

#ifndef TODOITEM_H
#define TODOITEM_H


#include <string>
#include <QString>
#include <QPushButton>
#include <QFrame>
#include <QDateTime>
#include "todolist.h"


namespace Ui {
    class ToDoItem;
}

class ToDoItem : public QFrame{
    Q_OBJECT

    public:
        //constructor and destructor
        explicit ToDoItem(QFrame *parent = 0);
        ~ToDoItem();

        //functions
        void setValues(QString mainText_,          // sets all import
                   QString additionalText_,
                   QDateTime dateTime_,
                   bool hasDeadline_);
        void paintEvent(QPaintEvent *pe);          //added to support stylesheets
        void setDeadline(QDateTime deadline_);
        QDateTime getDeadline();
        bool getHasDeadline();
        void setId(int id_);
        int getId();
        void setSecsTillDeadline();
        int getSecsTillDeadline();
        bool getSorted();
        void setSorted(bool sorted_);
        QString getMainText();
    private:
        QString formatTime(int duration_);
        Ui::ToDoItem *ui;
        int id;
        QDateTime deadline;
        bool hasDeadline;
        int secsTillDeadline;
        bool sorted;                               // set to true only during the sorting process
};

#endif // TODOITEM_H


推荐答案

普通循环包括问题发生在:

ah

The common circular include problem occurs with:
a.h

#ifndef A_H
#define A_H
#include "b.h"
// do something requiring content from b.h
#endif  

bh

#ifndef B_H
#define B_H
#include "a.h"
// whatever
#endif  

如果一些cpp包含ah 你可能会放弃它(如果bh真的不需要啊)。但是如果cpp改为包含b.h,那么b.h包含a.h BEFORE 之前声明a.h所需的内容。然后a.h尝试包括b.h但包含保护块,所以编译器处理a.h没有来自b.h的声明并失败。

If some cpp includes "a.h" you might get away with it (if b.h didn't really need a.h). But if that cpp instead includes b.h then b.h includes a.h BEFORE declaring things needed by a.h. Then a.h tries to include b.h but the include guard blocks that, so the compiler processes a.h without the declarations from b.h and fails.

这个常见的问题经常被另一个图层掩盖,就像在你的例子中:todoitem.h包含了todolist.h,即使它并不需要它,然后todolist.h包括intemmonitor.h,需要todoitem.h,但没有包括它,由于包括警卫。

That common problem is often masked by another layer, as it was in your example: todoitem.h included "todolist.h", even though it didn't really need it, then "todolist.h" included "intemmonitor.h" which needed "todoitem.h" but failed to include it due to the include guard.

这篇关于类未在范围中声明 - 即使包括.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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