Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: Create a new class with another parameter

Tags:

c++

qt

qlist

I have a problem in instantiating a new class with another parameter.

My problem is to add something in a QList Asteroids Object. In Qt, I have this error message:

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

Below are the relevant files of the class doing it, and I think 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));
    }
}

Constructor of Asteroid.c :

Asteroid::Asteroid(SettingsAsteroid settingsAsteroid)
like image 595
Linquisiteur Avatar asked Sep 11 '26 14:09

Linquisiteur


1 Answers

Based on your error

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

On the code:

this->settingsAsteroid = SettingsAsteroid();

You are attempting to convert a SettingsAsteroid into a SettingsAsteroid*, that is: a pointer to a SettingsAsteroid object.

Because GameView has a member settingsAsteroid which is a SettingsAsteroid*, you need to give it a pointer to a SettingsAsteroid, not a SettingsObject itself. You can do one of the following:

this->settingsAsteroid = new SettingsAsteroid();

Calling new will allocate memory for the required object (your SettingsAsteroid) and return a pointer to that memory, of type SettingsAsteroid*. Alternatively, if you already have some SettingsAsteroid object you could assign it instead:

SettingsAsteroid sa;
...
this->settingsAsteroid = &sa;
like image 166
Tas Avatar answered Sep 14 '26 05:09

Tas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!