c++:未定义引用

C++ : Undefined reference

本文关键字:引用 未定义 c++      更新时间:2023-10-16

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();
 };

在一个名为interp.cpp的类中的函数中:我正在尝试这样做:

window::setmove(4);

但遗憾的是,我得到了这个错误:

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

这很奇怪,因为在inter。cpp中的另一个函数中,我可以使用window::push_back(new_shape);

你知道有什么问题吗?谢谢你。

编辑:因为它被标记为重复:我不知道它如何可能是一个编译器问题,因为我能够使用窗口类内的其他函数。

c++需要静态成员的定义。在interp.cpp中定义静态变量move可以解决这个问题。您需要在inter .cpp中添加static int window::move;

感谢:MattMcNabb和juanchopanza的评论。