Hello everyone. I have been attempting to create a header file for the past 4 hours and failed miserably. I also read/tried multiple way of making said header file yet it wasn't functional. (I use Dev-c++)
I have my main program called something.cpp The said program work and compile and function properly. I made this program has a tool set. it contain function that I will use for a later stage of my prog.
if I understand correctly how to make a header, I put (let say) #include "module_o.h" a top my .cpp. I also create a module_0.h file (with a notepad) and copy paste the functions declarations (and the type def) inside that. Then I should be able to call upon the something.cpp functions if I add #include "module.o" to my main.cpp that will use said function. Well it doesn't really work.
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <WinConsole.h>
#define HAUTEUR 20
#define LARGEUR 80
#include"module_o.h"
typedefstruct {
int posix; //there 2 more type def
int posiy;
}NC;
//(followed by the functions. one exemple)
void NV_ctu(T_case nouv_ctu,T_case T_ocean[HAUTEUR][LARGEUR],int x,int y)
{
T_ocean[y][x].contenu=nouv_ctu.contenu;
T_ocean[y][x].numero=nouv_ctu.numero;
}
in module_o.h
1 2 3 4 5 6 7 8 9 10 11 12 13
#if !defined module_o
#define module_o
//typedef declaration the same as the something.cpp
typedefstruct {
int posix;
int posiy;
}NC;
//functions (there ~8 of them)
NC nouvelle_coord(T_case T_ocean[HAUTEUR][LARGEUR], int x, int y);
#endif
#ifndef module_ocean
#define module_ocean
[code]
Then take out the struct definition in your cpp because you already defined it in your header file so you don't need it again. You should also put all your includes in your header file, but that isn't important. The only header that is required in your cpp is the one that points to the header file itself you are writing.
To recap, move all the includes to your header except the #include "ocean.h" and take out the struct in the cpp.
Also, I create structs a little differently then you did here. I see a lot of people doing it your way but the way I do it is like this
[code]
struct NC
{
int posix;
int posiy
}
What you are doing does work, but generally typedef is suppose to define one thing as another like
typedef (void *) VOIDPOINTER
So everything you use VOIDPOINTER it will mean void * instead like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void OtherFunction (int y);
{
y++;
}
VOIDPOINTER GetSomething()
{
return &Otherfunction;
}
void main()
{
int x=0;
VOIDPOINTER myFunction = GetSomething();
myFunction(x);
cout << x; // x is now 1
}
typedef is great for declaring something a specific format so that everything must be that format. A great example is a Windows Wrapper and you want someone to create functions to pass in that wrapper that use the specific format
typedef WINDOWMESSAGE void(HWND, LPARAM, WPARAM);
like I said, you can use it the way you are and it won't cause you any problems, I am just not sure why people are being trained to use it like this when you are suppose to use it like you set up a class.
That is my bad. They are all called module_o even the file name are the same yet I get errors when I try to compile.
If I remove the typedef from the module_o.cpp and only leave those in the module_o.h, ill only get error about Largeur and hauteur not declared in this scope.
Thanks a lot for all the help. I can't really say why my teacher showed us to use the TypeDef the way I used them. maybe he is lacking some info about them.
I tried what you told me to do and I now get these message, that are not errors, just messages.
multiple definition of `clrscr()'
first defined here
multiple definition of `clreol()'
first defined here
multiple definition of `clreoscr()'
etc.
These are all function of the winconsole.h
Are those linker error?
EDIT:I put the winconsole.h inside the .cpp instead of the header and it seem to have fixed it.
Multiple definition generally means either something was declared twice like using the same name for a variable as a global and a local or in the situation where you include a header that is already included in another header. A perfect example is WinSock2.h and Windows.h WinSock2.h includes Windows.h so if you put it after Windows.h in your program, you will get a bunch of redefinition errors. Sometimes moving around your header file locations is all you need which you figured out.