I have two classess Game and ApButton and I want ApButton to use Game attributes so I want to make these two friend functions but I keep receiving the error:
`Game` does not name a type
I know that I should not have added apbutton.h in game class but I have to do it since game uses ApButton (a class inherited from pushbutton) do you have any other solution for this problem?
Here is the code for the two classes:
#ifndef GAME_H
#define GAME_H
#include <QtGui>
#include <QWidget>
#include <apbutton.h> //I have to add this
#include <QHBoxLayout>
#include <QTimer>
#include <iostream>
#include <QMouseEvent>
using namespace std;
namespace Ui {
class Game;
}
friend class ApButton;
class Game : public QWidget
{
Q_OBJECT
public:
explicit Game(QWidget *parent = 0);
~Game();
QLabel *bomb_label();
private:
Ui::Game *ui;
ApButton **btn; //that's why I have to include apbutton.h
};
#endif // GAME_H
#ifndef APBUTTON_H
#define APBUTTON_H
#include <QPushButton>
#include <iostream>
#include <QMouseEvent>
#include <game.h>
using namespace std;
class ApButton : public QPushButton
{
Q_OBJECT
public:
explicit ApButton(QWidget *parent = 0);
void setRowCol(int _row,int _col);
void mousePressEvent(QMouseEvent *ev);
private:
string name;
int row;
int col;
Game g; //here is the problem!
};
#endif // APBUTTON_H
I assume Ui::Game being your Qt-generated widget class, while Game is your implementing class. Your problem is the circular inclusion dependency (between "Game.h" and "ApButton.h"), which is usually solved using forward declarations. In fact, you are already using that mechanism for the Ui::Game class in "Game.h":
namespace Ui {
class Game;
}
Now just add below that:
class ApButton;
and remove:
#include <apbutton.h>
Unless you do not plan to use any methods of ApButton in the "Game.h" header and btn remains a pointer member (why double pointer here?), you are fine with the incomplete type.
Also your friend declaration
friend class ApButton;
belongs inside the Game class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With