0

I am trying to call the static function AreaMap::staticInitialize(Model *) from a method in the class View. It compiles when I define the class AreaMap first but I get the error show below when I try declaring View first even though I forward declared AreaMap. Anyone know a way to keep the definition of View at the top?

#ifndef VIEW_H
#define VIEW_H
#include "Model.h"

class AreaMap;

class View {
 public:
  void linkMvc(Model * m) {
    model = m;
    AreaMap::staticInitialize(m);
  }

  Model * model;
};

class AreaMap {
 public:
  void static staticInitialize(Model * m) {
    model = m;
  }
  Model * model;

};
#endif

Error:

inc/View.hpp: In member function ‘void View::linkMvc(Model*, Controller*)’:
inc/View.hpp:36:7: error: incomplete type ‘AreaMap’ used in nested name specifier
       AreaMap::staticInitialize(m);
user1505520
  • 405
  • 1
  • 7
  • 15

2 Answers2

0
#ifndef VIEW_H
#define VIEW_H

class AreaMap;

class View {
 public:
  void linkMvc(Model * m);

  Model * model;
};

class AreaMap {
 public:
  void static staticInitialize(Model * m) {
    model = m;
  }
  Model * model;

};

void View::linkMvc(Model * m) {
  model = m;
  AreaMap::staticInitialize(m);
}

#endif

However, this still shouldn't compile. staticInitialize() attempts to modify a non-static member variable.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • sorry spoke too soon. I am getting a linking error. It says multiple definitions of View::linkMvc – user1505520 Apr 08 '14 at 04:58
  • Did you remove the definition of `linkMvc` from the class definition? Without more information, it will be difficult to help. The code I've shown above (with a fix for the mentioned bug) does compile: http://ideone.com/D9f0g0 – Bill Lynch Apr 08 '14 at 04:59
  • obj/moc_View.o: In function `AreaMap::staticInitialize(Model*)': /usr/include/c++/4.8/ext/new_allocator.h:104: multiple definition of `View::linkMvc(Model*, Controller*)' – user1505520 Apr 08 '14 at 05:03
  • I am using g++ and compiling and linking separately. On the linking step I get the multiple definition error and following is "undefined reference to `AreaMap::model" – user1505520 Apr 08 '14 at 05:10
  • Ok. putting staticInitialize() in a View.cpp file got rid of the multiple definition error. I am still getting "undefined reference to `AreaMap::model" – user1505520 Apr 08 '14 at 05:17
  • Ok. Apparently you have to define it outside the class as well. [link](http://stackoverflow.com/questions/272900/undefined-reference-to-static-class-member) I added "Model * AreaMap::model;" to the View.cpp file and that fixed the undefined reference error. – user1505520 Apr 08 '14 at 05:29
0
class AreaMap;                       <----- This won't suffice.

class View {
 public:
  void linkMvc(Model * m) {          <----- What's Model?
    model = m;
    AreaMap::staticInitialize(m);    <----- What's this?
  }

  Model * model;
};

There are two problems here:

  • What's Model? You should be forward declaring it.
  • Your forward declaration of class AreaMap does not suffice for calling AreaMap::staticInitialize. You need to declare that function before you can reference it.
David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • Model is in another header file. Sorry there should be a #include "Model.h" at the top there. – user1505520 Apr 08 '14 at 04:59
  • And AreaMap::staticInitialize() is a static function defined in AreaMap. – user1505520 Apr 08 '14 at 05:01
  • @user1505520 - (1) If the header only needs to know that `Model` is some class, all that is needed is a forward declaration. But something is needed. (2) I know what `AreaMap::staticInitialize` is. I can read ahead to the next class definition. The problem is that the compiler doesn't read ahead like that. – David Hammen Apr 08 '14 at 08:08