Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class not declared in scope - even though .h was included

Tags:

c++

scope

include

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

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

the error is in this class definition:

#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

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
like image 485
user2145312 Avatar asked Nov 11 '15 12:11

user2145312


2 Answers

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  

b.h

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

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.

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.

like image 80
JSF Avatar answered Oct 26 '22 11:10

JSF


Be sure to check each header has a unique #define value associated with it.

This might happen when duplicating a header file but forgetting to change the #define HEADER_H to a unique identifier, and so the guard will stop the new header file from being included even when you #include it.

like image 36
jackw11111 Avatar answered Oct 26 '22 09:10

jackw11111