seperating function logic from main.cpp file...

15 Oct 2012

Hi all, As I continue forward with my project I am noticing my main.cpp file is getting quite large when it comes to the amount of lines my main.cpp contains. I created a cf.h file and copied then pasted the functions that require the ConfigFile library into the cf.h file. I left include "ConfigFile.h" library in the main.cpp file thinking the cf.h file could 'see' the functions in the ConfigFile.h library, but debug through errors. I fixed the errors by including the ConfigFile.h library into my cf.h file. If possible, how do I get my cf.h file to 'see' the functions in the ConfigFile.h when #include "ConfigFile.h" only exists in the main.cpp file? I would like to keep current and future #include somefile.h in my main.cpp file so other future .h files can use them when loading them once in the main.cpp. I am not sure if what I am trying to accomplish is considered a class? I wouldn't even know where to begin about writing a class if that is the case.

15 Oct 2012

Hi there. If I understand your post correctly, simply try including ConfigFile.h in cf.h. Basically, you cannot assume including a header in one section will automatically include it in any other files of your code.

Further, I hope you have included ifdef statements in your headers to guard against multiple definition errors.

Hopefully there are no typos as well with your code. If it's still no good, post a quick chunk of your code =] I can also provide you a link to some of my code as an example (my quadcopter project should have a nice example of multiple classes, files and libraries for you to explore).

Ian

15 Oct 2012

Hi Ian. I will gladly except your providing me a link to some of your code for a good example of multiple classes, files and libraries to explore. Including ConfigFile.h into my cf.h file worked as I expected. The issue that I am currently having is my cf.h file will also require me to include MbedJSONValue.h. Also, another .h file may also require MbedJSONValue.h in order to function correctly. Is it good practice to load the same .h file such as MbedJSONValue.h into other .h files, or does that matter? My thinking was to possibly include all my .h files in the main.cpp file once so other libraries and .h files can also use them, but from reading your response: "Basically, you cannot assume including a header in one section will automatically include it in any other files of your code." that is apparently not possible.

Rich

Ian Hua wrote:

Hi there. If I understand your post correctly, simply try including ConfigFile.h in cf.h. Basically, you cannot assume including a header in one section will automatically include it in any other files of your code.

Further, I hope you have included ifdef statements in your headers to guard against multiple definition errors.

Hopefully there are no typos as well with your code. If it's still no good, post a quick chunk of your code =] I can also provide you a link to some of my code as an example (my quadcopter project should have a nice example of multiple classes, files and libraries for you to explore).

Ian

15 Oct 2012

Hi Rich,

Here is the link to my current quadcopter code with the examples: Link.

Note that I will take down that repo after a few days, because that code is way in alpha stage; it's unclean, it's incomplete and just makes me feel bad. The main point is that you will see that I have a library for WiiExtensions and Servo. Those are included in my PrimaryHeader.h. Then, PrimaryHeader.h is declared across all my .cpp files :D

If that code is too hard to navigate, refer to this page; great example: Link. Should be extremely useful once my repo goes down.

Re MbedJSONValue.h: I see no problem including it across many files. In fact, my understanding is that we probably want to do that. For example, if I had some function called foo, I dont want to have to type it out in every single .cpp file where it is called. Rather, have a single header, and then point every .cpp file to that header; much neater, and definitely easier to debug.

Once you get to advance compiling, you will see that having it out as a header could be very time-efficient, since there is less code to compile: Source.

Hope this helps,

Ian =]

15 Oct 2012

Here is a link to my current demo project to learn how to use header files: http://mbed.org/users/d0773d/code/dothfiles/ That was my first attempt before I read your project example. I was able to narrow down my errors to only 2, but I am now officially stuck with my first attempt.

15 Oct 2012

Hi,

Thought also this might be of interest if you haven't sen it yet:

Hope tgat helps,

Simon