I'm including a header file (screen.h) in calss A that uses some of the functions that screen.h holds. Then I #include class A in class B. And I get redefinition errors. I have #ifndef's and #define's along with #endif's on all the headers. The function call is in the classes cpp file but thats not the definition.
Whats going on?
If I #include screen.h in class A's cpp file it works fine but I have to have those functions for my main.cpp... in which I get the redefinition erros again (once I include it).
What can I do about this? And why isn't #ifndef/#define macros working? Rather how do they work (everything works I just don't know it)
files are included into other files not classes. the ifndef/define/endif macros can be used as inclusion guards, which prevent something to be included more than once. you can still get redefinision errors if you use the same name twice, which is why you should group them into namespaces.
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
now I have class A's h file (classa.h) #include "fxn.h" to use say FunctionA. Then in main.cpp I have #include "ClassA.h" and #include "fxn.h". Now I'll get redefinition errors... Why shouldn't the compiler (MSVC++ 6) see it like this:
Code:
void FunctionA()
{
}
void FunctionB()
{
}
Class A
{
... blah - class stuff...
}
int main()
{
...blah blah blah...
}
but I believe its putting it in twice once above and once below class A... Any one know what I can do?
I need to use the same functions and renaming it and putting it in a different header isn't good code.
Theres gotta be something I can do?
If I include it before the include for the class it will go out of scope won't it?
Ok, so those are not compiler but linker errors! If I had known that beforehand...
The error is that you have function bodies in fxn.h. This way the function bodies are compiled in both cpp files, are written into both obj files and the linker doesn't know what to do with them.
Two solutions:
a) For small functions (like those in the fxn.h header) you might want to make them inline. Do that by simple putting the inline keyword in front of the return type.
b) For large functions you need another cpp where you put the function definitions in. Only the function prototypes remain in fxn.h.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.