链接 - c++函数指针奇怪的错误,为什么
天蓬老师
天蓬老师 2017-04-17 13:16:10
0
1
541
//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; }也通过了,我把那个函数指针换成其他函数也没问题,求解

天蓬老师
天蓬老师

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

membalas semua(1)
洪涛

找到说明了,,内联函数要定义在头文件中。。。。
编译时内联展开,不会产生函数,所以提示未定义的符号

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan