I'm trying to do a simple "Oriented object" programm in C and I come from Java. I did some research and reading online and I came with this code :
Declaring my base class
#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 x, 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
So this is the header file of my main structure called Figure
I'm not really sure about the last part where I define all method extern I think it's wrong. Then I want to do a figure_x
so I a figure_x.c
I did the following code to redefine the methods
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;
}
And finally I create a main.c
where I chose to use a static declaration and usage of my objet like this:
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;
}
Now I get the following error :
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
My question is how can be init_figure
undefined even if I use the key word extern