链接 - c++函数指针奇怪的错误,为什么
天蓬老师
天蓬老师 2017-04-17 13:16:10
0
1
535
//file:canvas_object.hpp
class CanvasObject
{
public:
    bool switch_shape() { return switch_shape(true); }
    bool switch_shape(bool order);//逆时针 or 顺时针
}
//file:canvas_object.cpp
inline bool CanvasObject::switch_shape(bool order)
{
    return true;
}
//file: canvas_view.cpp
//一个静态成员
bool (CanvasObject:: *CanvasView::_ctl_callfuncs[5])() = {
    &CanvasObject::move_down,
    &CanvasObject::move_down,
    &CanvasObject::move_left,
    &CanvasObject::move_right,
    &CanvasObject::switch_shape,
};
error:
Undefined symbols for architecture i386:
  "CanvasObject::switch_shape(bool)", referenced from:
      CanvasObject::switch_shape() in canvas_view.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我把那个函数定义中inline去掉编译就通过了,为什么呢?我试过直接让改成
bool switch_shape() { return true; }也通过了,我把那个函数指针换成其他函数也没问题,求解

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(1)
洪涛

I found the instructions, the inline function must be defined in the header file. . . .
Inline expansion during compilation does not generate a function, so an undefined symbol is prompted

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template