Qt: create new class with different parameter

I am having a problem creating a new class with a different parameter.

My problem is to add something to the QList asteroid object. In Qt, I have this error message:

cannot convert 'SettingsAsteroid' to 'SettingsAsteroid *' in this-> settingsAsteroid = SettingsAsteroid ();

Below are the relevant class files that do this and I think the other classes are not relevant.

Data in GameView.h:

#ifndef GAMEVIEW_H
#define GAMEVIEW_H

#include <QGraphicsView>
#include <QGraphicsItem>

#include <QApplication>
#include <QPushButton>
#include <QList>
#include <QObject>
#include <QString>

#include "Asteroid.h"
#include "SettingsAsteroid.h"


class GameView : public QGraphicsView
{
    Q_OBJECT

    // Data
    int nbAsteroids;
    int nbAsteroidsAlive;
    SettingsAsteroid* settingsAsteroid;
    QList<Asteroid> asteroids;


    // Menu
    QPushButton *startgame;

    // Scène
    QGraphicsScene* grfxScene;

public:
    GameView();
    ~GameView();

private slots:

    void start();

};

#endif // GAMEVIEW_H

      

Source code in GameView.c:

#include "GameView.h"
#include <iostream>


GameView::GameView()
{
    int nbAsteroids = 0;
    int nbAsteroidsAlive = 0;

    // data de jeu
    this->settingsAsteroid = SettingsAsteroid();

    //Scene de debut
    this->grfxScene = new QGraphicsScene();
    grfxScene->setSceneRect(0,0,800,600);
    this->grfxScene->addPixmap(QPixmap(":/images/armageddon.jpg"));

    setScene(this->grfxScene);

}

GameView::~GameView(){ }

void GameView::start()
{
    this->grfxScene->clear();

    int nbAsteroids = 4;
    int nbAsteroidsAlive = 4;

    int i;
    for(i=0;i<nbAsteroids;i++) {
       asteroids.append(new Asteroid(settingsAsteroid));
    }
}

      

Asteroid.c constructor:

Asteroid::Asteroid(SettingsAsteroid settingsAsteroid)

      

+3


source to share


1 answer


Based on your mistake

cannot convert ' SettingsAsteroid

to SettingsAsteroid*

' on assignmentthis->settingsAsteroid = SettingsAsteroid();

In code:

this->settingsAsteroid = SettingsAsteroid();

      

You are trying to convert SettingsAsteroid

to SettingsAsteroid*

, that is: a pointer to an object SettingsAsteroid

.



Since it GameView

has a member SettingsAsteroid

that is SettingsAsteroid*

, you need to point it to a pointer to SettingsAsteroid

, not to SettingsObject

. You can do one of the following:

this->settingsAsteroid = new SettingsAsteroid();

      

The call new

allocates memory for the desired object (yours SettingsAsteroid

) and returns a pointer to that type memory SettingsAsteroid*

. Alternatively, if you already have an object SettingsAsteroid

, you can assign it :

SettingsAsteroid sa;
...
this->settingsAsteroid = &sa;

      

+1


source







All Articles