0

I'm new to C++ and I'm trying to find the proper way to initialize this class.

Here is the code:

void NewDialog::createNew()
{
    InkSpot *ink;
    ink = new InkSpot(this);
    InkPuppet *puppet;
    puppet->ui->canvas->addWidget(ink->widget);
    close();
}

Now, for InkSpot *ink; & ink = new InkSpot(this); it works fine because I actually DO need a new InkSpot created.

But I do not need a new InkPuppet *puppet created, I simply need to refer to it's UI. Using the above code puppet->ui->canvas->addWidget(ink->widget); causes the program to crash because it is not being initialized.

The whole issue here is, that to initialize a new class I know how to do it, but initializing a pre-existing class I'm not sure how to refer to it.

newdialog.h

#ifndef NEWDIALOG_H
#define NEWDIALOG_H

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

#include <QDialog>

namespace Ui {
class NewDialog;
}

class NewDialog : public QDialog
{
    Q_OBJECT

public:
    explicit NewDialog(QWidget *parent = 0);
    ~NewDialog();
    InkPuppet *pointerToPuppet;

private:
    Ui::NewDialog *ui;

private slots:
    void createNew(InkPuppet *existingPuppet);

};

#endif // NEWDIALOG_H

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

#endif // INKPUPPET_H
4

2 回答 2

2

Well, what I meant with my above comment was, in the section/function where you call createNew(InkPuppet *existingPuppet) you should have something like:

// initialize the InkPuppet object
InkPuppet * existingPuppet = new InkPuppet();
// then call the function you have to create a new InkBlob for that InkPuppet

// (...) you probably have here some code to create/execute the dialog

dialog.createNew(existingPuppet);

All this assuming that the dialog you created is of the NewDialog class you have above. What you get with this is that your InkPuppet is initialized before and independently of the object you want to use it for. Think of it like using a knife to cut a steak before the steak exists - it wouldn't be possible.

Also, whenever you do this, you should check before passing the pointer as an argument, if the pointer actually exists. Usually something like:

if(pointer!=0)
{
    // pointer exists, do something
    functionThatUsesThePointerAsArg(pointer);
}

UPDATE:

I noticed in your code that you also created an InkSpot instance in your function. Note that this won't be available to your InkPuppet class unless you make it so in the NewDialog class. If you don't do this, and sticking to the previous metaphor, it's like the NewDialog class has a knife, and doesn't share it with anyone who might need, like InkPuppet, for instance.

In newdialog.h try something like:

class NewDialog : public QDialog
{
    Q_OBJECT

public:
    explicit NewDialog(QWidget *parent = 0);
    ~NewDialog();

    // add this:
    InkPuppet* pointerToPuppet;

private:
    Ui::NewDialog *ui;

private slots:
    void createNew(InkPuppet *existingPuppet);

};

And then, in the function description:

void NewDialog::createNew(InkPuppet * existingPuppet){
    // (...)
    this->pointerToPuppet = existingPuppet;
    // (...)
}

UPDATE2:

So the first problem I found is that your InkPuppet is a QMainWindow, so if you try to create a new instance of it inside of itself, that might explain why it blows up. :) So instead of what I suggested earlier, use maybe this instead of creating a new instance:

dialog.createNew(this);

As for the errors in the compiler, I can't actually figure them out, sorry. :( But I suspect it might relate to the fact that you might have deleted something you shouldn't... like the inkpuppet.h, maybe? that would explain why the compiler doesn't recognize the InkPuppet * existingPuppet; pointer as a type and tries to assume int.

于 2013-08-28T08:04:23.130 回答
0

I guess you may do that as follows since your purpose is to add ink into puppet. Is it? Hope it works for you:

void NewDialog::createNew()
{
    InkSpot *ink = new InkSpot();
    InkPuppet *puppet = new InkPuppet(this);
    puppet->ui->canvas->addWidget(ink->widget);
    close();
}

In your question you are not initializing puppet, which causes access violation when calling methods from the object.

于 2013-08-28T07:39:12.053 回答