Mam klasy Controller
oraz Actions
i inne. Kontroler ma w sobie Actions
i inne którymi zarządza (klasy widoków, modeli, etc.). Chcę umieścić klasę Actions
w kontrolerze aby zarządzał wszystkimi interakcjami. To się udaje bez problemu, to jest po prostu zwykłe wstawienie pola actions
. Ale niektóre interakcje są bardziej złożone i wymagają dostępu do kontrolera aby wykonał jeszcze pewne elementy na widoku, modelu, wstrzymał bądź uruchomił jakiś wątek, którym sam zarządza. Zatem chcę też umieścić wskaźnik do kontrolera w konstruktorze Actions
, żeby móc się do niego odwoływać.
Klasy wyglądają tak (zbędne funkcje zostały usunięte by nie zaciemniać):
controller.h
:
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include "actions.h"
class Controller {
private:
Actions actions = Actions::Actions(this);
}
#endif
actions.h
:
#ifndef ACTIONS_H
#define ACTIONS_H
class Actions {
public:
Actions(Controller* controller): controller(controller) {}
private:
Controller* controller;
}
#endif
No i prosty plik main.cpp
:
#include "controller.h"
int main() {
Controller controller;
return 0;
}
Niby prosta rzecz ale niestety otrzymuję następujące komunikaty o błędach:
In file included from controller.h:4:0,
from main.cpp:1:
actions.h:6:23: error: expected ‘)’ before ‘*’ token
Actions(Controller* controller): controller(controller) {}
^
actions.h:9:5: error: ‘Controller’ does not name a type
Controller* controller;
^~~~~~~~~~
actions.h:10:2: error: expected ‘;’ after class definition
}
^
;
In file included from main.cpp:1:0:
controller.h:9:2: error: expected ‘;’ after class definition
}
^
;
controller.h:8:44: error: cannot call constructor ‘Actions::Actions’ directly [-fpermissive]
Actions actions = Actions::Actions(this);
^
controller.h:8:44: note: for a function-style cast, remove the redundant ‘::Actions’
controller.h:8:44: error: no matching function for call to ‘Actions::Actions(Controller*)’
In file included from controller.h:4:0,
from main.cpp:1:
actions.h:4:7: note: candidate: constexpr Actions::Actions()
class Actions {
^~~~~~~
actions.h:4:7: note: candidate expects 0 arguments, 1 provided
actions.h:4:7: note: candidate: constexpr Actions::Actions(const Actions&)
actions.h:4:7: note: no known conversion for argument 1 from ‘Controller*’ to ‘const Actions&’
actions.h:4:7: note: candidate: constexpr Actions::Actions(Actions&&)
actions.h:4:7: note: no known conversion for argument 1 from ‘Controller*’ to ‘Actions&&’
Jak prawidłowo umieścić w kontrolerze obiekt Actions
, by ten miał wskaźnik waśnie do tego obiektu kontrolera?