How do you implement header guards, and what can you put between them?

The FILENAME_H is a convention. If you really wanted, you could use #ifndef FLUFFY_KITTENS as a header guard (provided it was not defined anywhere else), but that would be a tricky bug if you defined it somewhere else, say as the number of kittens for something or other. In the header file add.h the declarations … Read more

Why do we need both using-directives and include-directives?

using directives and include preprocessor directives are two different things. The include roughly corresponds to the CLASSPATH environment variable of Java, or the -cp option of the java virtual machine. What it does is making the types known to the compiler. Just including <string> for example will make you able to refer to std::string : … Read more

Are there any uses for unnamed namespaces in header files?

The only situation in which a nameless namespace in header can be useful is when you want to distribute code as header files only. For example, a large standalone subset of Boost is purely headers. The token ignore for tuples, mentioned in another answer is one example, the _1, _2 etc. bind placeholders are others.

In header files, what is the difference between a static global variable and a static data member?

Excuse me when I answer your questions out-of-order, it makes it easier to understand this way. When static variable is declared in a header file is its scope limited to .h file or across all units. There is no such thing as a “header file scope”. The header file gets included into source files. The … Read more

How do you correctly define entities within a namespace that have been declared in a header?

From a code readability standpoint, it is probably better in my opinion to use the #2 method for this reason: You can be using multiple namespaces at a time, and any object or function written below that line can belong to any of those namespaces (barring naming conflicts). Wrapping the whole file in a namespace … Read more

How to separate a class and its member functions into header and source files

The class declaration goes into the header file. It is important that you add the #ifndef include guards. Most compilers now also support #pragma once. Also I have omitted the private, by default C++ class members are private. // A2DD.h #ifndef A2DD_H #define A2DD_H class A2DD { int gx; int gy; public: A2DD(int x,int y); … Read more

Where are the headers of the C++ standard library

GCC typically has the standard C++ headers installed in /usr/include/c++/<version>/. You can run gcc -v to find out which version you have installed. At least in my version, there is no vector.h; the public header is just vector (with no extension), and most of the implementation is in bits/stl_vector.h. That’s the case on my Ubuntu … Read more

Is is a good practice to put the definition of C++ classes into the header file?

The answer depends on what kind of class you’re creating. C++’s compilation model dates back to the days of C, and so its method of importing data from one source file into another is comparatively primitive. The #include directive literally copies the contents of the file you’re including into the source file, then treats the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)