0

In: graphics.h:

 class window {
     friend class mouse;
 private:
     static int width;         // in pixels
     static int height;        // in pixels
     static vector<object> objects;
     static size_t selected_obj;
     static mouse mus;
     static int move;
 public:
     static void setmove (int move_) { window::move = move_; } //<--- problem
     static void push_back (const object& obj)
         { objects.push_back (obj); }
     static void setwidth (int width_) { width = width_; }
     static void setheight (int height_) { height = height_; }
     static void main();
 };

In one of my functions in a class called interp.cpp: I'm trying to do this:

window::setmove(4);

But sadly, I get this error:

interp.o: In function `window::setmove(int)':
/afs/cats.ucsc.edu/users/m/graphics.h:72: undefined reference to `window::move'

It's weird because in another function inside the interp.cpp, I'm able to use window::push_back(new_shape);

Any idea what could be wrong? Thank you.

EDIT: Because it's been marked as duplicate: I don't see how it could be a compiler issue because I'm able to use the other functions inside window class.

Mickey
  • 117
  • 1
  • 10
  • are you able to access other functions `setwidth`and `setheight` in `interp.cpp`? – Srini May 28 '14 at 06:25
  • Yes, I'm able to access both of them in interp.cpp. – Mickey May 28 '14 at 06:27
  • @juanchopanza I disagree with this marking of duplicate. The "duplicate" is a huge article that covers several distinct problems , all of which lead to the same error message, and it may not be clear for an inexperienced C++ coder which one applies – M.M May 28 '14 at 06:29
  • can you replace `window::move= move_;` with just `move= move_;` in `static void setmove (int move_) { window::move = move_; }` and see what happens. This may not fix the issue but resolution operator is unnecessary here. – Srini May 28 '14 at 06:31
  • 6
    @Raymond the error is with `window::move`. You never defined it. When you write `static int move;` this is a *declaration*, but not a *definition*. You must also provide a definition for it, in a .cpp file: `static int window::move;` . This applies to all of the static variables and functions. Functions can be defined inline; and variables can be defined inline in a few cases. – M.M May 28 '14 at 06:31
  • 2
    @MattMcNabb Maybe this one is more useful: http://stackoverflow.com/questions/272900/undefined-reference-to-static-class-member I will see if I can change it. – juanchopanza May 28 '14 at 06:33
  • Genius, thank you very much sir. I didn't even bother looking into the cpp file, but it became obvious when I saw width and height defined. – Mickey May 28 '14 at 06:34
  • @MattMcNabb, I think, your comment is actually an answer. – FreeNickname May 28 '14 at 07:38
  • this class has no rights to exist - if all class members and methods are static use namespace instead – zoska May 28 '14 at 07:59
  • @FreeNickname yes, I can't post it because this is marked as dupe tho – M.M May 28 '14 at 11:36

1 Answers1

-2

C++ requires a definition for your static members. Definition of the static variable move in interp.cpp would resolve the problem. You need to add static int window::move; in interp.cpp.

Credits to: MattMcNabb and juanchopanza for their comments.

Srini
  • 178
  • 2
  • 11
  • The comments and marking as duplicate were absolutely sufficient! – πάντα ῥεῖ May 28 '14 at 17:00
  • @πάνταῥεῖ : I did not know if comments are sufficient one need not answer. Thanks for letting me know this. I also felt that comments were sufficient. But I saw few answers (which are not there now) misleading. So I thought I could help the readers by mentioning the commentators along with the answer. – Srini May 29 '14 at 12:06