如何在C语言的main中使用对象外部方法?

我正在尝试用C语言编写一个简单的“面向对象”程序,我来自Java。我做了一些研究并在线阅读后,发现了以下代码:
声明我的基类

#ifndef FIGURE_H
#define FIGURE_H
struct figure{
    int(*m_draw) (struct figure* oref);
    int(*m_move) (struct figure* oref,int x,int y);
    int(*m_resize) (struct figure* oref,int y);
    int(*m_rotate) (struct figure* oref,int angle);
    int(*m_erase) (struct figure* oref);
    int(*m_show) (struct figure* oref);

};
extern void init_figure(struct figure* oref);

extern int draw (struct figure* oref);
//... so on

#endif //FIGURE

所以这是我主要结构的头文件Figure,我不确定最后定义所有方法extern的那一部分,我认为这是错误的。然后我想做一个figure_x,所以我做一个figure_x.c,下面的代码重新定义了方法
figure_x.c

#include <stdio.h>
#include "figure.h"

int f_draw(struct  figure* oref){
    printf("Hey I'm an awesome figure (%p),I bet you never have seen something like that b4",oref);
    //idk what to do but when I tried with void I had error and with int it's fine lol help me
    return -1;
}

int f_move(int x,int y,struct  figure* oref){
    return -1;
}

//so on for each method defined in the figure structure
void init_figure (struct figure* oref)
{
    oref->m_draw = f_draw;
    oref->m_move = f_move;
    oref->m_resize = f_resize;
    oref->m_rotate = f_rotate;
    oref->m_erase  = f_erase;
    oref->m_show   = f_show;
}

最后,我创建了一个main.c,在这里我选择使用静态声明和对象的用法,如下所示:
main.c

#include <stdio.h>
#include "figure.h"
int main() {
    //create a fig
    struct figure fig;
    //init the fig
    init_figure(&fig);
    fig.m_draw(&fig);
    fig.m_move(&fig,1,2);
    fig.m_resize(&fig,3,4);
    fig.m_rotate(&fig,5);
    fig.m_erase(&fig);
    fig.m_show(&fig);
    return 0;
}

现在出现以下错误:

C:\Users\xyz\AppData\Local\Temp\ccSxcgqa.o:main.c:(.text+0x15): undefined reference to `init_figure'
collect2.exe: error: ld returned 1 exit status

我的问题是,即使我使用关键字init_figure,如何extern也不确定

XHLIZIMING 回答:如何在C语言的main中使用对象外部方法?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3163906.html

大家都在问