Usually when you create a class in CodeBlocks you have a source file and a header file.
Well, it used to work, but now whenever I create a new class, it doesn't have ".h" or ".ccp" in it. The text is plain and there's no syntax highlighting on it. Here's the image:
Related
Can we remove .h extensions while we define our own header file in c++? like in case of standard header files in c++.
I have created a header file and named it add.h and tried including it using #include "add" but it didn't work.
after following up the comments and answers:
using codeblocks ide
i have created a "add" of type File and tried it including in my source file and it worked. attaching the snapshot below.
the aim of my question is to ask if userdefined header files can also omit .h extensions and how?
i am really trying to explore this fact and don't have a good understanding of how compilers stores standard header files.
A easy to understood conclusion is really appreciated
Thankyou.
Can we remove .h extensions while we define our own header file in c++?
Sure, as long as that matches the filename of the file. As far as the language is concerned, the name of the file is largely irrelevant.
However, .h or similar such as .hpp is conventional, and helps the reader of the source to understand what the file is used for. This is an important consideration.
Another consideration is that some tools use the filename as a heuristic to determine the purpose of the file. For example, your IDE might not assume that the file contains C++ code, and thus not enable C++ features such as source analysis unless you follow a common naming convention.
I have created a header file and named it add.h and tried it including in source file using #include "add" but it didn't work.i know i am missing some important concepts here.
What you're missing is that the include directive must match the name of the file. If you include "add", then you must name the file add, not add.h. If you name a file add.h, then you must include "add.h", not "add".
Can we remove .h extensions while we define our own header file in c++? like in case of standard header files in c++.
You've misunderstood how the files in the stardard library are named. The header file iostream is actually named iostream and not iostream.hpp or iostream.h (unless you use a very old compiler).
I have created a header file and named it add.h and tried including it using #include "add" but it didn't work.
The reason that doesn't work is because the pre-compiler tries to read the file add and you've named the file add.h.
I am using C++ to do a homework assignment. I am using templates for my class, so I need all implementations of template methods to be in .ipp, not .cpp file (I can't write implementation in .h file as it is the assignment submission requirement)
Eclipse does not seem to support .ipp files without additional settings. There is not much information about .ipp files on the internet, I could not find anything helpful.
I defined a new file type in Eclipse preferences to be able to create an .ipp file. Now that I am trying to write my methods definitions in .ipp file, it keeps complaining that "member declaration is not found" even though I write #include *.ipp" at the end of my header file.
What could the problem?
You have to register .ipp as C++ source file extension:
In Window > Preferences: C/C++ > File Types click New...
Enter the pattern *.ipp and as type select C++ Source File
Click OK
Click Apply and Close
In code blocks their default file is main.cbp so I usually change it to main.cpp. But there doesn't seem to be a difference between their performances. But then again I just began coding in C++ so I'd like to know if there are any differences before I get too deep.
.cbp is the extension for a codeblocks solution file. Usually the project file will contain the .cpp file. chp files dont contain the actual source code but the procedure for codeblocks to associate files.
In a nutshell, .cpp contains the source code while cbp files dont.
Why do you have to know about that?
Answer: when passing source code, cpp file is the only format that can be opened for IDE other than codeblocks eg. Dev C++
I know it is completely stupid question, but when I received a sample application supposed to run on Linux, I have got some .cpp files, with no .h files, even-though that inside the .cpp file there is a mentioning of some .h files, is it Ok? or the files are really missing and I should ask the supplier -which is a big company- to provide me with those missing .h files??
Sample of the files I have received:
The file name is: XXX.cpp
#include "XXX.h"
XXX::XXX(bool aEnableLogging /*= true*/) :
abcd(aEnableLogging)
{
//SOME CODE
}
XXX::~XXX()
{
//SOME CODE
}
bool XXX::Run()
{
//SOME CODE
}
.
.
.
Another question, can I easily write the .h files manually using .cpp? and what is the header file XXX.h for this XXX.cpp file for example??
PS. I am slightly new to C++, and I am trying to build this application using MS VS2010, can I do this? or I need clips or another Linuxy IDE?
Yes, you need the header files.
You may be able to infer some of what is in them, but you will not be able to fully rewrite them yourself. For example, just by looking at this file we have no idea whether XXX::Run() should be public, private, or protected. It's also possible that there are inline functions/methods or even macros in the header file that don't appear in the source files.
I have added some objective C files and c++ files to an existing iOS project. When I import a .h file of a .mm file into a .h file with a corresponding .m file I get all kinds of errors.
When I change the .m file of the .h file importing the new code to .mm I then get the same previous errors but with new errors regarding the new .mm file.
Im not really sure how to fix this.
I am using ARC if this has any effect.
It complains about namespace (unknown type), also Unknown name issues with an imported .mm file, expected expression error on a statement containing :: in a .h file of a .mm file. What I dont get is that these files work correctly in the project I copied them over from. I only took the source from the project. And have added header search paths to the directory. I also selected create references to the added directories option.
For some reason the files arent in the compile source section, and when I try to add them, the file selector tree doesnt contain these source files I added. The added directory is also blue instead of yellow, but empty.
As you know, C++ code can only be used from .mm files. When the compiler complains about namespace being an unknown type, it typically means that the Objective C compiler is being used when the Objective C++ compiler should be used. More simply: you have a .m file that needs to be a .mm.
Speaking generally, there are two options you can pursue. Every time an Objective C file complains, change it to a .mm. This is a slippery slope, and generally results in your entire project cascading into a giant ball of Objective C++. The other option is to quarantine the C++, so that only a bare minimum of your Objective C files need the .mm extension.
Obviously, the latter is the better choice. The likely cause of the confusion is #importing a header file that contains C++ code from elsewhere in your app that doesn't actually require C++. If the C++ is an implementation detail of an Objective C class, you can try to keep the C++ out of the class' header file by leveraging Class Extensions. Otherwise, you can create an Objective C class whose only purpose is to wrap the C++ in its own implementation, while exposing a clean header file for other classes to consume.
Basically, If you can keep your headers clean, then you won't have to worry about accidentally indirectly #importing some C++ into an unsuspecting compilation unit.