I have been looking up on this one for long, but I couldn't solve it.
What I have been trying to do is to "split" my source files into .h and .cpp files as conventionally done. Another thing I tried doing, as advised by another programmer, is including all the dependencies of a file within itself. The error I am getting is a "Multiple Definition of" type of error. The thing is I've been using the "#ifndef" pre-processor stuff to make sure everything is included only once, yet I am getting the Multiple Definition errors with functions and variables in those files 0_o. I already know I shouldn't include any .cpp inside any of the .h files nor should I define functions there. I have done a lot of research, but still can't seem to figure this one.
I am pretty sure I am doing wrong with the all including stuff; just couldn't figure out where >.<
None the less: not constant global variables shouldn't be in header files.
Oh! Is there another place I should place them in then? A cpp perhaps?
But then I can't #include that cpp, right?
Thanks again!
Edit:
Alright! Rearranged everything according to Disch's article. My .h files' content are now the stuff encapsulated with the include guards. Removed all the non-constant variables from globals.h
~Bump. Updated with more information and questions. I didn't want to double post, but when I updated, the thread was already on its way to the next page.
You can clearly see I've got those functions defined!!? Now why can't the linker find them?
I can't see any reason if the file is properly added to the project and hence involved in the build process. So it looks like there's something wrong with your project.
Edit 2: And is it okay to have initial definitions for the class members in the class declaration itself? I mean something like:
C++11 allows it.
if you want to share global variables do that with extern:
x.h: externint x;
x.cpp: int x = 123;
Having a lot of global variables is not a good idea anyway.
Thanks for answering my question! I have already removed all the non-constant variables from that .h file and just shared the values through the functions.
I did. The source code was actually working and compiling perfectly before I did this splitting. Just double checked, and I am sure the SDL library is properly installed. It's actually the only external one I am using currently.
Just where am I doing wrong ?_? I did my best to follow Disch article and now this happens.
For code::blocks:
Go to the menu 'Project', select 'Porperties...'
A dialog 'Project/targets options' appears. Press the button 'Project's build options...'
A dialog 'Project build options' appears. Select the tab 'Linker settings'. What libraries appear in the list box 'Link libraries:'?
You might need to adjust the 'Search directories'/'Linker' there as well
Yes! You were right! I did have those in the global compiler settings, and I thought that would just be for every project. But once I did that in the Project's options, it compiled and linked perfectly!
On DevCpp the case was different though; it was just the IDE being stupid; I forgot to #include <algorithm> for std::sort in Interface.cpp
Anyway, this was quite weird and very unstable. Now I am facing a problem with C::B, and apparently, it is failing to read a file through fstream. Debugging and watching a string causes a segmentation fault immediately 0_o.
The same code compiles and works with DevCpp however. That iss getting off topic, so I might just start another thread.
Thank you very very much! I appreciate all your time and effort!