Recently, I have been trying to break out of some bad coding habits. I have been trying to improve my coding style and my over all program design and organization. One of my bad habits has been writing everything in header files and simply including them in main.cpp. By this, I mean that main.cpp has been my only source file, and every other file in my project has been a header.
Clearly, this is a problem, and I am trying to break out of it and write my future projects with separate source files and headers for each class. However, because I am not accustomed to breaking up my files in this way (and because I never learned the correct way to do it), I am having trouble.
My current understanding is that I write the class declaration in the header file, for example:
MyClass.h
Code:#ifndef __MYCLASS_H__ #define __MYCLASS_H__ class MyClass { private: int a_, b_, c_; public: MyClass(); int getA(void); int getB(void); int getC(void); }; #endif /* __MYCLASS_H__ */
And then write the method definitions in a *.CPP file which includes the *.H file, like so:
MyClass.cpp
This is all well and good -- the code compiles and runs as expected. It even works fine when I want to use another object -- I simply include its header file in the source file of the object I want to use with it. Oddly enough, however, once my project comes to involve more than three or four objects, I encounter some very frustrating compile time errors. They will either claim I've declared something twice (despite my careful wrapping of headers such that they can't be compiled twice), or they will claim the opposite -- that I have failed to declare the object in question at all. Please note that while I am not above simply overlooking something obvious, I have been doing this for a while and have a decent idea of how preprocessor directives work -- I realize that the order in which files are included matters.Code:#include "myclass.h" MyClass::MyClass() { a_ = 54; b_ = 92; c_ = 0; } int MyClass::getA(void) { return a_; } int MyClass::getB(void) { return b_; } int MyClass::getC(void) { return c_; }
Some of the errors I have gotten have included "[class name] is not a type",
"ISO C++ forbids declaration of [class name] without a type" (apparently it thinks I'm trying to declare a variable, when in fact I'm trying to declare an object of the type it thinks is the variable name), as well as errors claiming I have multiple declarations of the same class. Adding extra inclusion directives at different places has hit-and-miss effectiveness, adding empty classes of the same name as the ones being ignored, such as:
amusingly will cause the compiler to act as though the empty class was declared first (although the inclusion directive which includes the file that has the real class declaration in it is right ABOVE the empty class).Code:class MyClass { };
Bottom line, I don't know everything, clearly, but I'm not stupid. I don't understand what's going on here. My IDE is the latest (buggy) version of Dev-Cpp, and my compiler is the GNU compiler that I downloaded alongside Dev.
Thank you for reading, and any help would be greatly appreciated.