05-20-2012 03:02 PM
I come from .Net development, and I'm quite new to C++. Sometimes I use objects that are not included in the "include" regions at the header of the cpp file. For example, to use CCPoint, I need to include a header, but sometimes it's tricky to know what is the necessary header.
Is there any way to add the headers automatically? In .Net we have the "resolve namespace" feature that adds the necessary "usings" at the top of the c# classes.
05-22-2012 03:19 AM
I don't know such a feature. In C/C++ it's also difficult to do such things, cause every file could have the definition of many classes with defines used for definition of classnames. That's the advantage and disadvantage of a precompiler. So the only way I know is to use a search-engine at the internet.
05-22-2012 09:10 AM
By convention, most classes are defined in a .h file of the same name.
So if you need ListView you will #include <bb/cascades/ListView>, and if you need QPoint you will #include <QPoint>.
Find an example of something in the right subsystem and you'll know what to include.
Other standard types are well documented everywhere, including the linux command "man <thing>". The documentation should tell you what to include, and you'll quickly learn them.
This is a feature you don't want. #include in C++ is not the same as #import in other languages. In C++, each cpp file is compiled separately, and each #include effectively inserts the code inline. This leads to two issues:
1) (typically not an issue any more) Lots of includes means lots of file I/O and compile time per cpp file
2) Cascaded includes affects incremental build times. If a.h includes b.h includes c.h includes... then everything that includes a.h will be recompiled if you touch z.h.
Occasionally someone has defined include files that must be included in a particular order.
Often C++ coders like to see system include files first, 3rd party include files second, followed by their own.
So here's the usual approach:
- .h files should refer to classes by reference or pointer where possible, and provide a forward declaration,
- .cpp files that actually require the full definition should #include the definition.
- exception is small well encapsulated classes designed to be passed by value, e.g. QPoint and QString.
05-22-2012 12:16 PM
I take back my "no", but bear in mind the rest of my comments.
Let's say you have C++ code like:
QmlDocument* qml = QmlDocument::create("main.qml");
and you haven't added the include file for it.
In Momentics, click on QmlDocument, then either:
<right-mouse context menu>/Source/Add Include
This will add the required include for this class.