Jul 8, 2011 at 1:19pm UTC
I have a header file that i'd like to keep the definitions in another .cpp file. How can I do that with #ifndef/#define ... or is it even needed when definitions are in a seperate .cpp file?
Jul 8, 2011 at 1:52pm UTC
Like this?
a.h
1 2 3 4 5 6 7 8 9 10 11 12
#ifndef _A_
#define _A_
class A
{
public :
//
private :
//
};
#endif
a.cpp
1 2
#include "a.h"
// Definitions of A-class
Last edited on Jul 8, 2011 at 1:53pm UTC
Jul 8, 2011 at 2:32pm UTC
Yes, what if I also seek to use this in, let's say Main.cpp?
Last edited on Jul 8, 2011 at 2:33pm UTC
Jul 8, 2011 at 2:41pm UTC
You include header file (this time a.h) in the main.cpp. Then you just compile with:
g++ a.cpp main.cpp -o program
Last edited on Jul 8, 2011 at 2:41pm UTC
Jul 8, 2011 at 6:13pm UTC
you should separate the Abstract Data Type to you main program.
you will just:
#include "a.h"
Jul 9, 2011 at 2:22pm UTC
Can you please elobare a bit? I have three files
header file (.h)
#ifndef/#define part
...Class and method prototypes...
#endif
definition file (.cpp)
#include (said header file)
...Define the functions
and lasty I have main.cpp that I want to use the defined functions. Is there a way or can I just remove the #ifndef/#define/#endif part?
eraggo, is that for another compiler? I use VS2008.
Last edited on Jul 9, 2011 at 2:23pm UTC
Jul 9, 2011 at 3:18pm UTC
Yes you can remove #'s (of course not in #include-statelement). Them just makes so if file is already included it won't do it again.
I use g++ as compiler under Linux.
Last edited on Jul 9, 2011 at 3:19pm UTC
Jul 9, 2011 at 4:22pm UTC
I found the sinner giving me errors -- declaration of global variable under the namespace in the header. Is it okay if I only declare it in the cpp defining? For I won't have to deal with it directly from main.