Reputation: 972
I am implementing a card game. I want the window ui
to be an object of class Rule
, so that Rule
can modify the GUI directly.
So Rule
has an object Window * ui
initialized in the constructor.
But during compilation, the compiler tells me that in rule.h
, "Window has not been declared", "Window is not a type". I included <window.h>
, everything has the same type, everything is initialized, so I run out of ideas of why it does not work.
EDIT: I edited the code following alexisdm's notes. In addition, in rule.h
, I had to change the definition of ui
from Window * ui
to Ui::Window *ui
, because in window.h
, ui
is defined as Ui::Window * ui
, and this is the object I want Rule::rule
to control.
But now, in rule.cpp, ui->do_stuff()
does not compile, because Ui::Window
does not have the do_stuff()
function, but Window
does...
Arg! What to do?!! ui->do_stuff() works fine in window.cpp. Inheritance problem?
window.cpp:
Window::Window(QWidget *parent) :
QDialog(parent),
ui(new Ui::Window)
{
ui->setupUi(this);
player = new Game_state(); /* Game state */
rule = new Rule(player, ui); /* Implement the action of cards*/
}
void Window::do_stuff(){
//Do stuff
}
window.h
#include <QDialog>
#include "game_state.h"
#include "rule.h"
class Rule;
namespace Ui {
class Window;
}
class Window : public QDialog
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
~Window();
Game_state * player;
Rule * rule;
void do_stuff();
private:
Ui::Window *ui;
};
rule.h
#ifndef RULE_H
#define RULE_H
#include "window.h"
#include <game_state.h>
class Window;
class Rule{
public:
Rule(Game_state *, Ui::Window *);
void action(Card *);
private:
Game_state * player;
Ui::Window * ui;
};
#endif // RULE_H
rule.cpp
#include <rule.h>
Rule::Rule(Game_state * game, Ui::Window * window){
player = game;
ui = window;
}
void Rule::stuff(){
ui->do_stuff();
}
Upvotes: 0
Views: 2015
Reputation: 29896
There is a circular dependency in your headers: window.h
and rule.h
are referencing each other.
You should replace both #include
by forward declarations to avoid it.
class Window; // instead of #include "window.h"
class Rule; // instead of #include "rule.h"
You'll still need to add #include "window.h"
in rule.cpp
, and #include "rule.h"
in window.cpp
.
Upvotes: 1
Reputation: 12832
My guess is the name of your class, Window
, is too generic and #include <window.h>
probably includes the system one instead of yours.
Try change the clase and header name to something more specific to your app like CardWindow
, or use #include "window.h"
Upvotes: 0