0

I'm from a java background and I'm trying to learn C++ with QT trying to make a tic tac toe game. I have problems with initializing objects within a certain class : I want the MainWindow class to have a Player instance and initialize Player by calling it's constructor but I don't understand the errors

#ifndef PLAYER_H
#define PLAYER_H

#include "board.h"
#include <qstring.h>
class Player
{
public:
    QString token;
    Player(QString);
    void jouerCoup(int,int, Board&);
};

#endif // PLAYER_H

And this is the MainWindow class

#include <qstring.h>
#include "player.h"
#include "board.h"
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    Player aPlayer;
private:
    Ui::MainWindow *ui;
private slots:
     void buttonHandle();
};

#endif // MAINWINDOW_H

In MainWindow.cpp I try this

aPLayer = new Player("X");

and I get this error :

../tictactoe/mainwindow.cpp: In constructor 'MainWindow::MainWindow(QWidget*)':
../tictactoe/mainwindow.cpp:6:26: error: no matching function for call to 'Player::Player()'
 ui(new Ui::MainWindow)

I tried making the QString mutable, I have also a constructor in Player.cpp that takes a QString and assign it to the Player's member.

Any indication as to what I should do next ? Can I initialize Player in the MainWindow definition directly ?

1 Answer 1

2

The problem could be that you declare your aPlayer member variable as being a Player object, however you initialize it as if it is a pointer to Player object. You should either declare it as a pointer:

Player *aPlayer;

or in the MainWindow class contructor initialize it as:

MainWindow::MainWindow(QWidget *parent)
    :
        QMainWindow(parent),
        aPlayer("X")
{}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.