Получение нескольких ошибок при создании модели в Qt - PullRequest
0 голосов
/ 18 мая 2011

Я создаю класс в Qt. Но все работало, пока я не построил класс настольной модели. Теперь я получаю ошибки "expected ( before * token"Creator does not name a type". В чем проблема? Это кажется очень загадочным.

#ifndef OPENMODEL_H
#define OPENMODEL_H

#include <QAbstractTableModel>
#include <QString>
#include <QObject>

#include "creator.h"

namespace language
{
    class OpenModel : public QAbstractTableModel
    {
        Q_OBJECT

    public:
        explicit OpenModel(Creator* creator, QObject *parent = 0); // Creater* throws a expected ) before * token

        // QAbstractTableModel Model view functions
        int rowCount(const QModelIndex &parent = QModelIndex()) const ;
        int columnCount(const QModelIndex &parent = QModelIndex()) const;
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
        QVariant headerData(int section, Qt::Orientation orientation, int role) const;

        // QAbstractTableModel Model edit functions
        bool setData(const QModelIndex & index, const QVariant & value, int role);
        Qt::ItemFlags flags(const QModelIndex &index) const;

        // Functions to manipulate creator
        void add(QString name, QString file);
        void remove(int index);

        // Functions to move files up and down
        void moveup(int index);
        void movedown(int index);

    private:
        Creator* creator; // Creator does not name a type

    };
}

#endif // OPENMODEL_H

это создатель.h

/*
  This is the main file for the language-creator

  It controls the addition, deletion and change of the centances (files)
  It shall be passed by pointer to the models to be proccessed
  */

#ifndef CREATOR_H
#define CREATOR_H

#include <QObject>
#include <QVector>

#include "file.h"
#include "openmodel.h"
#include "setmodel.h"

namespace language
{
    class Creator
    {

    public:
        Creator();

        void addFile(const File& f); // Adds a file to the vector
        bool removeFile(int index); // Remove a file from the vector
        bool replaceFile(int index, const File& f); // Replaces a file at index

        const QVector<File>* getFiles() const; // Returns a list of the files

        OpenModel getOpenModel() const; // Returns a pointer to the open model
        SetModel getSetModel() const; // Returns a pointer to the set model

        void reset(); // This resets the class to an initialized state

    private:
        QVector<File> files; // This holds all the files
    };
}

#endif // CREATOR_H

Ответы [ 2 ]

3 голосов
/ 18 мая 2011

У вас есть циклическая ссылка между этими заголовочными файлами. openmodel.h включает creator.h и наоборот. Таким образом, когда creator.cpp (я предполагаю, что такой файл существует) компилируется, он будет включать openmodel.h до объявления класса Creator (помните, что #include означает, что содержимое файла будет вставлено прямо там ), следовательно, вы получите ошибку.

Чтобы избежать этого, вы можете удалить #include "creator.h" из openmodel.h и вместо этого добавить предварительную декларацию:

class Creator;

Поместите объявление прямо перед классом OpenModel. Поскольку в этом классе вы используете только указатели на Creator, это будет нормально работать.

0 голосов
/ 18 мая 2011

Ваш creator.h файл содержит openmodel.h, который использует идентификатор Creator до того, как createor.h изменил его, чтобы объявить его.

Поместить class Creator; форвардную декларацию в openmodel.h.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...