C++ Class Error (no matching function call)

So I am writing a mini (very mini) game in C++ and Qt like the space shooting game where you shoot ships, although a bit modified. So there are two classes, one called Health, and one EnemyHealth. Health and EnemyHealth are identical, one for the player and one for the computer, but one works and the other does not. When I try and build it it outputs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

/home/nanoandrew4/QtProjects/QtMovingWithArrowKeys/Game.cpp:42: error: no matching function for call to 'EnemyHealth::EnemyHealth()'
 enemyHealth = new EnemyHealth();
                               ^

/home/nanoandrew4/QtProjects/QtMovingWithArrowKeys/EnemyHealth.h:8: candidate: EnemyHealth::EnemyHealth(QGraphicsItem*)
     EnemyHealth(QGraphicsItem *parent);
     ^
/home/nanoandrew4/QtProjects/QtMovingWithArrowKeys/EnemyHealth.h:8: note:   candidate expects 1 argument, 0 provided

/home/nanoandrew4/QtProjects/QtMovingWithArrowKeys/EnemyHealth.h:6: candidate: EnemyHealth::EnemyHealth(const EnemyHealth&)
 class EnemyHealth: public QGraphicsTextItem{
       ^
/home/nanoandrew4/QtProjects/QtMovingWithArrowKeys/EnemyHealth.h:6: note:   candidate expects 1 argument, 0 provided


I have looked at the classes, made sure that there were no spelling/common mistakes (or what I see as common). I can't see what I am doing wrong, and can not apply any other solutions to my problem. It is also late so my thinking is not as good. My question is how to fix it, but more importantly, how to avoid it in the future? Also any pro tips to make the code neater/better?

Game.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include "Player.h"
#include <QTimer>
#include "Enemies.h"
#include "Game.h"
#include "Health.h"
#include "EnemyHealth.h"

extern int enemyNum;

Game::Game(QWidget *parent){

//create scene
scene = new QGraphicsScene(); //creates scene
scene->setSceneRect(0,0,800,600); //sets size of scene so it is not infinite (scrollbar)

setScene(scene); //scene pointer created in header file
setFixedSize(800,600); //sets size of view, not scene
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

player = new Player(); //player pointer created in header file
//set size and location of player
player->setRect(0,0,100,100);
//make rectangle focusable
player->setFlag(QGraphicsItem::ItemIsFocusable);
player->setFocus(); //sets the rectangle to be focused on
//add rectangle item to scene
scene->addItem(player);
player->setPos(((scene->width()/2) - player->rect().width() / 2), scene->height() - (10 + player->rect().height()));
//sets starting location to the middle of the screen (x), plus half the width of the square to make it centered, and the bottom of the screen, plus a bit to make it entirely visible

//create score
score = new Score();
scene->addItem(score);
health = new Health();
health->setPos(health->x(), health->y()+50);
scene->addItem(health);
enemyHealth = new EnemyHealth(); //this is offending line
enemyHealth->setPos(enemyHealth->x()+600, enemyHealth->y()+50);
scene->addItem(enemyHealth);

//spawn enemies
//QTimer *timer = new QTimer();
//QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn())); //every 2s (2000ms), send timeout signal and run spawn function in player class
//timer->start(2000);

if(enemyNum < 1){
    Player bo;
    bo.spawn();
    enemyNum++;
}


show();
}


Game.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef GAME
#define GAME

#include <QApplication>
#include <QWidget>
#include <QGraphicsScene>
#include <QGraphicsView>
#include "Player.h"
#include "Score.h"
#include "Health.h"
#include "EnemyHealth.h"

class Game: public QGraphicsView {

public:
    Game(QWidget * parent=0);
    QGraphicsScene *scene;

    Player *player;
    Score *score;
    Health *health;
    EnemyHealth *enemyHealth;


};

#endif // GAME



Health.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "Health.h"
#include <QFont>

Health::Health(QGraphicsItem *parent) : QGraphicsTextItem(parent)
{
    health = 3;

    //draw text
    setPlainText("Health: " + QString::number(health));
    setDefaultTextColor(Qt::green);
    setFont(QFont("times", 24));

}

void Health::decrease()
{
    health--;
    setPlainText("Health: " + QString::number(health));

}

int Health::getHealth()
{
    return health;
}


Health.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef HEALTH
#define HEALTH

#include <QGraphicsTextItem>

class Health: public QGraphicsTextItem{
public:
    Health(QGraphicsItem * parent=0);
    void decrease();
    int getHealth();
private:
    int health;

};


#endif // HEALTH


EnemyHealth.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "EnemyHealth.h"
#include <QFont>

EnemyHealth::EnemyHealth(QGraphicsItem *parent) : QGraphicsTextItem(parent)
{
    enemyHealth = 3;

    //draw text
    setPlainText("Enemy Health: " + QString::number(enemyHealth));
    setDefaultTextColor(Qt::red);
    setFont(QFont("times", 24));

}

void EnemyHealth::decrease()
{
    enemyHealth--;
    setPlainText("Enemy Health: " + QString::number(health));

}

int EnemyHealth::getEnemyHealth()
{
    return enemyHealth;
}


EnemyHealth.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef ENEMYHEALTH
#define ENEMYHEALTH

#include <QGraphicsTextItem>

class EnemyHealth: public QGraphicsTextItem{
public:
    EnemyHealth(QGraphicsItem *parent);
    void decrease();
    int getEnemyHealth();
private:
    int enemyHealth;

};

#endif // ENEMYHEALTH


(*note, game in progress, so commented out lines can be ignored, this is just for testing purposes or just to use as reference if I need to in the future, and some might not be descriptive of the object, as I have not updated some comments alongside the code. Also, I just started with this so don't be surprised if you find some massive mistakes)
Last edited on
Morning,
You've given your class a constructor:
EnemyHealth(QGraphicsItem *parent);
which is completely fine. However, I think once you'ce supplied your own the compiler then does not supply a default one (i.e one that takes zero parameters). it only supplies the default one if you don't supply anything in the way of constructors.
So when you come to do this:
enemyHealth = new EnemyHealth();
You're going to get an error as you have no supplied a ctor that takes zero parameters.
So you need to either supply one that takes zero parameters, or if you have one to hand, pass in QGraphicsItem pointer one line 42.
Last edited on
Topic archived. No new replies allowed.