Hello, i am a beginner and i've recently now split my code into Header files, implementation files and a main file after initially having all the code in one file. This worked fine until now when i am looking at 'inheritance' for the first time and i've got into a slight mess. I'm convinced i've only made a very small error , not with the code itself but with the inclusion of header files and the way it's all linked. Basically the program inherates the the features of a rectangle class and makes a new box class(i'm sure you've all seen this exercise before!!) .For this i have the follwing files..
reactange.h - contains rectangle class
box.h - contains box defintion including inheritance instruction
rectangleImp.cpp - contains member functions for reactangle.h
boxImp.cpp - contains additional member functions for box class
main - creating box objects
All sit neatly inside a project enviroment without any obvious problems.But when i 'build' the project, it reports 'rectangle redefintion' errors..
Obviously, i cant upload all the files to you , nor would i want to bore you all with it!!.
I'm looking for similar simple exanmple that i can view that contains the same level of complexity to see how it is done. I've checked out the source code on the web site and i can't find one suitable??. Below is the files again with what i believe to be the header files that should be called out
rectangle.h - no header file
box.h - rectangle.h included
rectangleImp.cpp - rectangle.h included
boxImp.cpp -rectangle.h & box.h included
main -rectangle.h and box.h included
or.. is it that the header files are being refered to too often??, and i should be making use of the #ifndef command.. more effectively?
Thanks for that link , it's a little clearer..however
I'm a little confused over naming conventions for header guards
If I have class called myClass in a header called myClass.h
should the file be referred to as H_myClass . What is the syntax exactly?
If I have class called myClass in a header called myClass.h
should the file be referred to as H_myClass
Huh? You can refer to it however you want? But if the file is "myClass.h" then it makes sense to refer to it as "myClass.h".
But I get the feeling I'm not understanding your question.
I'm also not sure what "forward declaring" is?
It's sort of like function prototyping. It tells the compiler that the class exists so that you can use the name of the class in your code, but it doesn't actually give any details of the class (so you can't create objects of that class).
example programs:
1 2 3 4
int main()
{
Foo* p; // ERROR, compiler doesn't know what 'Foo' is
}
with forward declaring:
1 2 3 4 5 6
class Foo; // a forward declaration
int main()
{
Foo* p; // now this is OK, because Foo was foreward declared.
}
I mean what is the naming convention for header file
immediately after the #ifndef command?. It seems to change from example to example. Should it be myclass_H (in my example) ??. There are always some underscores in the name?
It doesn't matter at all. The only thing that matters is that it's unique. It's never actually used anywhere in code, so it can be a random jumbling of letters for all anyone cares.
If your filename is something generic and there might be the possibility for another header to have the same guard name, then you might want to elaborate further.