Qt / C ++ - “包含”头文件会导致很多错误(垃圾邮件)

时间:2013-08-30 08:32:10

标签: c++ qt

我的脚本遇到了一些问题。一切都很好,但现在当我#include一个文件时,我会在指向另一个类的任何行上发生错误。像这些(实际上有12个......):

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

当我说InkPuppet *pointerToPuppet;

时发生

编辑:我不能放入标题的包含是inkpuppet.h标题中的inkspot.h。

对于每个指针,我得到上面两个错误x3。

然后我得到错误,因为显然指针不再有效。

我将继续发布我的所有代码,因为我不知道哪些部分是相关的。

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();
}

2 个答案:

答案 0 :(得分:4)

您有循环依赖项。 “inkpuppet.h”包括“inkspot.h”,其中包括“newdialog.h”,其中包括“inkpuppet.h”。改为使用前向声明,尽可能使用。

答案 1 :(得分:4)

你有一个循环包含,由你的标题监护人阻止。这意味着在你的newdialog.h中,行#include "inkpuppet.h"什么都不做,导致InkPuppet类没有被声明。您需要通过前向声明class InkPuppet;替换include行,并且不要忘记将该文件包含在cpp中(在这种情况下,您已经拥有它)。

编辑:你必须小心命名空间,你已经将所有类都放在两个名称空间中(全局和Ui)。