0

I'm trying to implement MVC pattern in C++. I follow the instructions in the file: http://www.laputan.org/pub/papers/POSA-MVC.pdf

But when I compile program I gets this errors:

In file included from C:\Users\Lukasz\ClionProjects\CDDB\Controller.h:10:0,
                 from C:\Users\Lukasz\ClionProjects\CDDB\Controller.cpp:5:
C:\Users\Lukasz\ClionProjects\CDDB\View.h:17:5: error: 'Controller' does not name a type
     Controller *myController;
     ^
C:\Users\Lukasz\ClionProjects\CDDB\View.h:28:13: error: 'Controller' does not name a type
     virtual Controller *makeController() {}
             ^
C:\Users\Lukasz\ClionProjects\CDDB\View.h:33:5: error: 'Controller' does not name a type
     Controller *getController() { return myController; }
     ^
C:\Users\Lukasz\ClionProjects\CDDB\View.h: In constructor 'View::View(Model*)':
C:\Users\Lukasz\ClionProjects\CDDB\View.h:19:34: error: class 'View' does not have any field named 'myController'
     View(Model *m) : myModel(m), myController(0)
                                  ^
In file included from C:\Users\Lukasz\ClionProjects\CDDB\View.h:10:0,
                 from C:\Users\Lukasz\ClionProjects\CDDB\View.cpp:5:
C:\Users\Lukasz\ClionProjects\CDDB\Controller.h:17:22: error: expected ')' before '*' token
     Controller( View *v )  : myView(v){
                      ^
C:\Users\Lukasz\ClionProjects\CDDB\Controller.h:27:5: error: 'View' does not name a type
     View    *myView;
     ^

My files:

View.h

#ifndef CDDB_VIEW_H
#define CDDB_VIEW_H


#include "Observer.h"
#include "Model.h"
#include "Controller.h"

class View : public Observer {


public:
    Model *myModel;
    Controller *myController;

    View(Model *m) : myModel(m), myController(0)
    {
        myModel->attach(this);
    }
    virtual ~View() {  }
    virtual void update() { }

    virtual void initialize() {}

    virtual Controller *makeController() {}

    virtual void draw();

    Model *getModel() { return myModel; }
    Controller *getController() { return myController; }

};

class BarChartView : public View {

public:
    BarChartView(Model *m) : View(m) { }
    virtual void draw();
};

#endif //CDDB_CONTROLLER_H

Model.h

#ifndef CDDB_MODEL_H
#define CDDB_MODEL_H

#include <iostream>
#include <cstdlib>
#include <list>
#include <set>
#include <iterator>
#include "Observer.h"

using namespace std;

class Model{

    string cdName;

public:
    Model(string cdName);

    void addRecord();
    void removeRecord();
    void attach(Observer *s) { registry.insert(s); }
    void detach(Observer *s) { registry.erase(s); }

protected:
    virtual void notify();

private:
    set<Observer*> registry;
};

#endif //CDDB_CONTROLLER_H

Controller.h

#ifndef CDDB_CONTROLLER_H
#define CDDB_CONTROLLER_H


#include "Observer.h"
#include "View.h"

class Controller : public Observer {

public:
    virtual void handleEvent() {}

    Controller( View *v )  : myView(v){
        myModel = myView->getModel();
        myModel->attach(this);
    }

    virtual ~Controller() { }
    virtual void update() { }

protected:
    Model   *myModel;
    View    *myView;

};


#endif //CDDB_CONTROLLER_H

Model.cpp, View.cpp, Controller.cpp are empty. I compile this code using cmake

cmake_minimum_required(VERSION 3.3)
project(CDDB)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp Model.h Observer.h View.h controller.h Controller.cpp Controller.h Model.cpp View.cpp Observer.cpp)
add_executable(CDDB ${SOURCE_FILES})

Any ideas?

EDIT:

Ok, I add class Model; class View; into Controller.h but I have new error 25: error: invalid use of incomplete type 'class View' myModel = myView->getModel();

lukassz
  • 3,135
  • 7
  • 32
  • 72
  • Looks like you need to use forward-declarations. – Dai Jan 14 '16 at 20:35
  • 1
    Your `#include`s are circular. – Biffen Jan 14 '16 at 20:35
  • @Biffen I don't understand. – lukassz Jan 14 '16 at 20:37
  • 1
    @lukassz `View.h` includes `Controller.h`, `Controller.h` includes `View.h`. https://en.wikipedia.org/wiki/Circular_dependency#Example_of_circular_dependencies_in_C.2B.2B – Biffen Jan 14 '16 at 20:38
  • `Controller.h` include `View.h` because constructor from Controler usage `View` how to resolve this? – lukassz Jan 14 '16 at 20:40
  • 1
    @lukassz See [the link](https://en.wikipedia.org/wiki/Circular_dependency#Example_of_circular_dependencies_in_C.2B.2B) in my previous comment. – Biffen Jan 14 '16 at 20:41
  • Ok, I add `class Model; class View;` into `Controller.h` but I have new error `25: error: invalid use of incomplete type 'class View' myModel = myView->getModel();` – lukassz Jan 14 '16 at 20:47
  • @Biffen I have a problem, when I add `class Model;` I don't to execute method from object. @up – lukassz Jan 14 '16 at 21:15
  • @lukassz You should put implementations in `.cpp` files, and just have declarations in `.h` files. – Biffen Jan 15 '16 at 06:12

0 Answers0