Search code examples
c++qtincludeheader-files

Including a header file causes a lot of errors


When I #include a file, I get errors that occur on any line pointing to another class. Like these (there's actually 12 of them..):

x:\development\inkpuppet\newdialog.h:20: error: C2143: syntax error : missing ';' before '*'
x:\development\inkpuppet\newdialog.h:20: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int

It occurs when I write InkPuppet *pointerToPuppet;.

The include I can't put in the header is inkspot.h in the inkpuppet.h header.

For each pointer I get the two above errors x3, then I get errors because obviously the pointers no longer work.

I do not know which parts of my code will be relevant.

inkpuppet.h

#ifndef INKPUPPET_H
#define INKPUPPET_H

#include "inkspot.h"
//#include "ui_inkpuppet.h"

#include <QMainWindow>
#include <QWidget>

namespace Ui {
class InkPuppet;
}

class InkPuppet : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit InkPuppet(QWidget *parent = 0);
    ~InkPuppet();

    Ui::InkPuppet *ui;

private slots:
    void setMinimum(int value);
    void setMaximum(int value);
    void actionNew();
    void actionAbout();
    void setSpacing(int);
    void on_colorAButton_clicked();
};

#endif // INKPUPPET_H

inkpuppet.cpp

#include "inkpuppet.h"
#include "ui_inkpuppet.h"
#include "newdialog.h"
#include "aboutdialog.h"
#include "inkspot.h"


#include <Qt>
#include <QtCore>
#include <QtGui>
#include <QWidget>
#include <QDialog>
#include <QMainWindow>
#include <QDebug>
#include <QColorDialog>


InkPuppet::InkPuppet(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::InkPuppet)
{
    ui->setupUi(this);
    //connect the frame range boxes to the timeslider
    connect(ui->lowerFrameBox, SIGNAL(valueChanged(int)), this, SLOT(setMinimum(int)));
    connect(ui->upperFrameBox, SIGNAL(valueChanged(int)), this, SLOT(setMaximum(int)));

    //connect tool palette items
    connect(ui->spacingBox, SIGNAL(valueChanged(int)), this, SLOT(setSpacing(int)));

    connect(ui->colorAButton, SIGNAL(clicked()), this, SLOT(on_colorAButton_clicked()));

    //connect the menu items
    connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(actionNew()));
    connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(actionAbout()));

}

void InkPuppet::setMinimum(int value)
{
    ui->timeSlider->setMinimum(value);
    ui->frameNumberBox->setMinimum(value);
}

void InkPuppet::setMaximum(int value)
{
    ui->timeSlider->setMaximum(value);
    ui->frameNumberBox->setMaximum(value);
}


void InkPuppet::setSpacing(int)
{
    InkSpot *spot = new InkSpot(this);
    //spot->puppet = this;
    spot->spacing = ui->spacingBox->value();
}

//menu items
void InkPuppet::actionNew()
{
    NewDialog *nDialog = new NewDialog;
    //nDialog->createNew(this);
    nDialog->pointerToPuppet = this;
    nDialog->setModal(true);

    nDialog->show();

}

void InkPuppet::actionAbout()
{
    AboutDialog *aDialog = new AboutDialog;
    aDialog->setModal(true);
    aDialog->show();
}


//tool menu
void InkPuppet::on_colorAButton_clicked()
{
//    QColor color = QColorDialog(QColor->setRgb(255, 0, 0));
//    //color->setRgb(0, 0, 0);
//    qDebug() << "done";
//    if(color->isValid())
//    {

//        QString qss = QString("background-color: %1").arg(color->name());
//        ui->colorAButton->setStyleSheet(qss);
//    }
}


InkPuppet::~InkPuppet()
{
    delete ui;
}

inkspot.h

#ifndef INKSPOT_H
#define INKSPOT_H

#include "newdialog.h"
//#include "inkpuppet.h"

#include <QObject>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QLabel>
#include <QPoint>
#include <QImage>


namespace Ui {
class InkSpot;
}

class InkSpot : public QWidget
{
    Q_OBJECT
public:
    explicit InkSpot(QWidget *parent = 0);
    void draw(QPainter *painter);
    QWidget *widget;
    int canvasWidth;
    int canvasHeight;
    float spacing;
    NewDialog *newDialog;
    QPixmap pixmap;

signals:
    //int sendWidthOfCanvas();
    //void sendHeightOfCanvas();
    
public slots:
    //void widthOfCanvas();
    //void heightOfCanvas();

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);


private:
    void drawLineTo(const QPoint &endPoint);
    bool drawing;
    QPoint lastPoint;
    QImage image;
    QImage test2;


    Ui::InkSpot *ui;
    
};

#endif // INKSPOT_H

inkspot.cpp

#include "inkspot.h"
#include "inkpuppet.h"
#include "ui_inkpuppet.h"

#include "newdialog.h"
#include "ui_newdialog.h"

#include <QtCore>
#include <QtGui>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>

InkSpot::InkSpot(QWidget *parent) :
    QWidget(parent)
{
    widget = this;
    drawing = false;
    //spacing = puppet->ui->spacingBox->value();
    spacing = 1;//puppet->ui->spacingBox->value();
}

void InkSpot::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        lastPoint = event->pos();
        drawing = true;
    }
}

void InkSpot::mouseMoveEvent(QMouseEvent *event)
{
    if((event->buttons() & Qt::LeftButton) && drawing)
    {
        drawLineTo(event->pos());
    }
}

void InkSpot::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton && drawing)
    {
        drawLineTo(event->pos());
        drawing = false;
    }
}

void InkSpot::drawLineTo(const QPoint &endPoint)
{
    QPainter painter(&pixmap);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::NoBrush);

    QFile *stencilInput; // file for input, assumes a SQUARE RAW 8 bit grayscale image, no JPG no GIF, no size/format header, just 8 bit values in the file
    char *brushPrototype; // raw brush prototype
    uchar *brushData; // raw brush data

    stencilInput = new QFile("C:/brush3.raw");  // open raw file
    stencilInput->open(QIODevice::ReadOnly);
    QDataStream in;
    in.setDevice(stencilInput);
    int size = stencilInput->size();  // set size to the length of the raw file

    brushPrototype = new char[size];  // create the brush prototype array
    in.readRawData(brushPrototype, size);  // read the file into the prototype
    brushData = new uchar[size];  // create the uchar array you need to construct QImage

    for (int i = 0; i < size; ++i)
        brushData[i] = (uchar)brushPrototype[i];  // copy the char to the uchar array

    QImage test(brushData, 128, 128, QImage::Format_Indexed8); // create QImage from the brush data array
     // 128x128 was my raw file, for any file size just use the square root of the size variable provided it is SQUARE
    QImage test2(128, 128, QImage::Format_ARGB32);


   QVector<QRgb> vectorColors(256);  // create a color table for the image
    for (int c = 0; c < 256; c++)
        vectorColors[c] = qRgb(c, c, c);

    test.setColorTable(vectorColors);  // set the color table to the image

    for (int iX = 0; iX < 128; ++iX)  // fill all pixels with 255 0 0 (red) with random variations for OIL PAINT effect
    // use your color of choice and remove random stuff for solid color
    // the fourth parameter of setPixel is the ALPHA, use that to make your brush transparent by multiplying by opacity 0 to 1
    {
        for (int iY = 0; iY < 128; ++iY)
        {
            test2.setPixel(iX, iY, qRgba(255, 100, 100, (255-qGray(test.pixel(iX, iY)))*0.5));
        }
    }
    // final convertions of the stencil and color brush
    QPixmap testPixmap = QPixmap::fromImage(test2);
    QPixmap testPixmap2 = QPixmap::fromImage(test);

    painter.setBrush(Qt::NoBrush);
    painter.setPen(Qt::NoPen);
    // in a paint event you can test out both pixmaps
//    QLineF line = QLineF(lastPoint, endPoint);
//    float lineLength = line.length();
//    qDebug() << line.length();
//    line.setLength(100.01f);
//    qDebug() << line.length();

    QPainterPath path = QPainterPath(lastPoint);
    path.lineTo(endPoint);
    //qDebug() << path.currentPosition();
    //qDebug() << path.length();
    qreal length = path.length();
    qreal pos = 0;

    while (pos < length)
    {
        qreal percent = path.percentAtLength(pos);
        painter.drawPixmap(path.pointAtPercent(percent).x() - 16, path.pointAtPercent(percent).y() - 16, 32, 32, testPixmap);
        pos += spacing;
    }


    //painter.drawPixmap(line.p1().x() - 16, line.p1().y() - 16, 32, 32, testPixmap);

    //delete all dynamically allocated objects with no parents
    delete [] brushPrototype;
    delete [] brushData;
    delete stencilInput;


    lastPoint = endPoint;

}


void InkSpot::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::NoBrush);
    QRect rect = event->rect();
    painter.drawPixmap(rect, pixmap, rect);

    qDebug() << spacing;

    update();

}

newdialog.h

#ifndef NEWDIALOG_H
#define NEWDIALOG_H

//#include "ui_newdialog.h"
#include "inkpuppet.h"

#include <QDialog>

namespace Ui {
class NewDialog;
}

class NewDialog : public QDialog
{
    Q_OBJECT
    
public:
    explicit NewDialog(QWidget *parent = 0);
    ~NewDialog();
    InkPuppet *pointerToPuppet;
    Ui::NewDialog *ui;

public slots:
    //void createNew(InkPuppet *existingPuppet);
    void createNew();

};

#endif // NEWDIALOG_H

newdialog.cpp

#include "newdialog.h"
#include "ui_newdialog.h"
#include "inkpuppet.h"
#include "ui_inkpuppet.h"
#include "inkspot.h"


#include <QDebug>
#include <QSignalMapper>
#include <QObject>
#include <QtOpenGL/QGLWidget>


NewDialog::NewDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewDialog)
{
    ui->setupUi(this);

    connect(ui->buttonBox, SIGNAL(accepted()), SLOT(createNew()));
    //connect(ui->buttonBox, SIGNAL(accepted()), SLOT(createNew(InkPuppet*)));

}

void NewDialog::createNew()
{
    InkSpot *ink;
    ink = new InkSpot();
    ink->newDialog = this;
    pointerToPuppet->ui->canvas->addWidget(ink->widget);
    //QSize size = pointerToPuppet->ui->canvas->sizeHint();
    ink->widget->resize(ui->widthBox->value(), ui->heightBox->value());
    pointerToPuppet->ui->scrollCanvasItems->resize(ui->widthBox->value(), ui->heightBox->value());
    //pointerToPuppet->ui->canvas->setAlignment(ink->widget, Qt::AlignAbsolute);
    ink->pixmap = QPixmap(QSize(ui->widthBox->value(), ui->heightBox->value()));
    close();
}

NewDialog::~NewDialog()
{
    delete ui;
}

main.cpp

#include "inkpuppet.h"
#include "inkspot.h"
#include "newdialog.h"
#include "aboutdialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    InkPuppet w;
    w.show();
    
    return a.exec();
}

Solution

  • You have circular dependencies. "inkpuppet.h" includes "inkspot.h", which includes "newdialog.h", which includes "inkpuppet.h". Use forward declarations instead, where you can.