- The first problem is that extern is only valid before the name of a type, so that's what the compiler is complaining about. The second is that cout is defined in the namespace std, so you need to tell the compiler to look there. The good thing is that the code doesn't say using namespace std.
- C program to demonstrate endl in a program to prompt the user to enter his or her name while using endl to print each statement in the next new line while flushing the output stream: Code: //The header file iostream is imported to enable us to use cout in the program.
- Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '0' at the end of the string when it initializes the array. Let us try to print above-mentioned string −.
- Dev-C is a free full-featured integrated development environment (IDE) distributed under the GNU General Public License for programming in C and C.It was originally developed by Colin Laplace and first released in 1998. It is written in Delphi. It is bundled with, and uses, the MinGW or TDM-GCC 64bit port of the GCC as its compiler.Dev-C can also be used in combination.
- Dev C%2b%2b Cout Does Not Name A Type
- Dev C 2b 2b Cout Does Not Name A Type B
- Dev C 2b 2b Cout Does Not Name A Type 1
I'm using vectors in a class that creates different attributes for different types of units and I'm getting all kinds of errors like does not name a type, expected ',' or '.' Before header file, the errors don't go away.
# Hello World
This program prints Hello World!
to the standard output stream:
Dev C%2b%2b Cout Does Not Name A Type
See it live on Coliru.
# Analysis
Let's examine each part of this code in detail:
Dev C 2b 2b Cout Does Not Name A Type B
Here, the `int` is what is called the function's [return type](http://stackoverflow.com/documentation/c%2B%2B/572/lambdas/1855/specifying-the-return-type#t=20170126184705835537).The value returned by the `main` function is an **exit code.**
By convention, a program exit code of `0` or `EXIT_SUCCESS` is interpreted as success by a system that executes the program. Any other return code is associated with an error.If no `return` statement is present, the `main` function (and thus, the program itself) returns `0` by default. In this example, we don't need to explicitly write `return 0;`.All other functions, except those that return the `void` type, must explicitly return a value according to their return type, or else must not return at all.`std::cout << 'Hello World!' << std::endl;` prints 'Hello World!' to the standard output stream:Dev C 2b 2b Cout Does Not Name A Type 1
- `std` is a [namespace](http://stackoverflow.com/documentation/c%2B%2B/495/namespaces#t=201701261847520776598), and `::` is the **scope resolution operator** that allows look-ups for objects by name within a namespace.There are many namespaces. Here, we use `::` to show we want to use `cout` from the `std` namespace. For more information refer to [Scope Resolution Operator - Microsoft Documentation](https://msdn.microsoft.com/en-us/library/b451xz31.aspx/).
- `std::cout` is the **standard output stream** object, defined in `iostream`, and it prints to the standard output (`stdout`).
- `<<` is, **in this context**, the **stream insertion operator**, so called because it **inserts** an object into the **stream** object.
The standard library defines the `<<` operator to perform data insertion for certain data types into output streams. `stream << content` inserts `content` into the stream and returns the same, but updated stream. This allows stream insertions to be chained:`std::cout << 'Foo' << ' Bar';`prints 'FooBar' to the console.
- `'Hello World!'` is a **character string literal**, or a 'text literal.' The stream insertion operator for character string literals is defined in file `iostream`.
- [`std::endl`](http://en.cppreference.com/w/cpp/io/manip/endl) is a special **I/O stream manipulator** object, also defined in file `iostream`. Inserting a manipulator into a stream changes the state of the stream.The stream manipulator `std::endl` does two things: first it inserts the end-of-line character and then it flushes the stream buffer to force the text to show up on the console. This ensures that the data inserted into the stream actually appear on your console. (Stream data is usually stored in a buffer and then 'flushed' in batches unless you force a flush immediately.)An alternate method that avoids the flush is:
where
n
is the character escape sequence for the newline character. - The semicolon (
;
) notifies the compiler that a statement has ended. All C++ statements and class definitions require an ending/terminating semicolon. - Explicit documentation of code to make it easier to read/maintain
- Explanation of the purpose and functionality of code
- Details on the history or reasoning behind the code
- Placement of copyright/licenses, project notes, special thanks, contributor credits, etc. directly in the source code.
- They must be maintained to reflect any changes in the code
- Excessive comments tend to make the code less readable
- The C++ preprocessor copies the contents of any included header files into the source code file, generates macro code, and replaces symbolic constants defined using #define with their values.
- The expanded source code file produced by the C++ preprocessor is compiled into assembly language appropriate for the platform.
- The assembler code generated by the compiler is assembled into appropriate object code for the platform.
- The object code file generated by the assembler is linked together with the object code files for any library functions used to produce an executable file.
- Note: some compiled code is linked together, but not to create a final program. Usually, this 'linked' code can also be packaged into a format that can be used by other programs. This 'bundle of packaged, usable code' is what C++ programmers refer to as a library.
- Note: C++ functions are essentially procedures and do not follow the exact definition or rules of mathematical functions.
- Note: popular function definitions may be hidden in other included files (often for convenience and reuse across many files). This is a common use of header files.
- The return type is
int
. - The name of the function is
add2
. - The number of arguments to the function is 1:
- The first argument is of the type
int
. - The first argument will be referred to in the function's contents by the name
i
.
A comment is a way to put arbitrary text inside source code without having the C++ compiler interpret it with any functional meaning. Comments are used to give insight into the design or method of a program.
There are two types of comments in C++:
# Single-Line Comments
The double forward-slash sequence //
will mark all text until a newline as a comment:
# C-Style/Block Comments
The sequence /*
is used to declare the start of the comment block and the sequence */
is used to declare the end of comment. All text between the start and end sequences is interpreted as a comment, even if the text is otherwise valid C++ syntax. These are sometimes called 'C-style' comments, as this comment syntax is inherited from C++'s predecessor language, C:
In any block comment, you can write anything you want. When the compiler encounters the symbol */
, it terminates the block comment:
The above example is valid C++ (and C) code. However, having additional /*
inside a block comment might result in a warning on some compilers.
Block comments can also start and end within a single line. For example:
# Importance of Comments
As with all programming languages, comments provide several benefits:
However, comments also have their downsides:
The need for comments can be reduced by writing clear, self-documenting code. A simple example is the use of explanatory names for variables, functions, and types. Factoring out logically related tasks into discrete functions goes hand-in-hand with this.
# Comment markers used to disable code
During development, comments can also be used to quickly disable portions of code without deleting it. This is often useful for testing or debugging purposes, but is not good style for anything other than temporary edits. This is often referred to as “commenting out”.
Similarly, keeping old versions of a piece of code in a comment for reference purposes is frowned upon, as it clutters files while offering little value compared to exploring the code's history via a versioning system.
# The standard C++ compilation process
Executable C++ program code is usually produced by a compiler.
A compiler is a program that translates code from a programming language into another form which is (more) directly executable for a computer. Using a compiler to translate code is called compilation.
C++ inherits the form of its compilation process from its 'parent' language, C. Below is a list showing the four major steps of compilation in C++:
Many C++ compilers may also merge or un-merge certain parts of the compilation process for ease or for additional analysis. Many C++ programmers will use different tools, but all of the tools will generally follow this generalized process when they are involved in the production of a program.
The link below extends this discussion and provides a nice graphic to help.[1]: http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html
# Function
A function is a unit of code that represents a sequence of statements.
Functions can accept arguments or values and return a single value (or not). To use a function, a function call is used on argument values and the use of the function call itself is replaced with its return value.
Every function has a type signature -- the types of its arguments and the type of its return type.
Functions are inspired by the concepts of the procedure and the mathematical function.
Functions are often meant to perform a specific task. and can be called from other parts of a program. A function must be declared and defined before it is called elsewhere in a program.
# Function Declaration
A function declaration is declares the existence of a function with its name and type signature to the compiler. The syntax is as the following:
In the example above, the int add2(int i)
function declares the following to the compiler:
The argument name is optional; the declaration for the function could also be the following:
Per the one-definition rule, a function with a certain type signature can only be declared or defined once in an entire C++ code base visible to the C++ compiler. In other words, functions with a specific type signature cannot be re-defined -- they must only be defined once. Thus, the following is not valid C++:
If a function returns nothing, its return type is written as void
. If it takes no parameters, the parameter list should be empty.
# Function Call
A function can be called after it has been declared. For example, the following program calls add2
with the value of 2
within the function of main
:
Here, add2(2)
is the syntax for a function call.
# Function Definition
A function definition* is similar to a declaration, except it also contains the code that is executed when the function is called within its body.
An example of a function definition for add2
might be:
# Function Overloading
You can create multiple functions with the same name but different parameters.
Both functions are called by the same name add2
, but the actual function that is called depends directly on the amount and type of the parameters in the call. In most cases, the C++ compiler can compute which function to call. In some cases, the type must be explicitly stated.
# Default Parameters
Default values for function parameters can only be specified in function declarations.
In this example, multiply()
can be called with one or two parameters. If only one parameter is given, b
will have default value of 7. Default arguments must be placed in the latter arguments of the function. For example:
# Special Function Calls - Operators
There exist special function calls in C++ which have different syntax than name_of_function(value1, value2, value3)
. The most common example is that of operators.
Certain special character sequences that will be reduced to function calls by the compiler, such as !
, +
, -
, *
, %
, and <<
and many more. These special characters are normally associated with non-programming usage or are used for aesthetics (e.g. the +
character is commonly recognized as the addition symbol both within C++ programming as well as in elementary math).
C++ handles these character sequences with a special syntax; but, in essence, each occurrence of an operator is reduced to a function call. For example, the following C++ expression:
is equivalent to the following function call:
All operator function names start with operator
.
While in C++'s immediate predecessor, C, operator function names cannot be assigned different meanings by providing additional definitions with different type signatures, in C++, this is valid. 'Hiding' additional function definitions under one unique function name is referred to as operator overloading in C++, and is a relatively common, but not universal, convention in C++.
# Visibility of function prototypes and declarations
In C++, code must be declared or defined before usage. For example, the following produces a compile time error:
There are two ways to resolve this: putting either the definition or declaration of foo()
before its usage in main()
. Here is one example:
However it is also possible to 'forward-declare' the function by putting only a 'prototype' declaration before its usage and then defining the function body later:
The prototype must specify the return type (void
), the name of the function (foo
), and the argument list variable types (int
), but the names of the arguments are NOT required.
One common way to integrate this into the organization of source files is to make a header file containing all of the prototype declarations:
and then provide the full definition elsewhere:
and then, once compiled, link the corresponding object file foo.o
into the compiled object file where it is used in the linking phase, main.o
:
An “unresolved external symbol” error occurs when the function prototype and call exist, but the function body is not defined. These can be trickier to resolve as the compiler won't report the error until the final linking stage, and it doesn't know which line to jump to in the code to show the error.
# Preprocessor
The preprocessor is an important part of the compiler.
It edits the source code, cutting some bits out, changing others, and adding other things.
In source files, we can include preprocessor directives. These directives tells the preprocessor to perform specific actions. A directive starts with a # on a new line.Example:
The first preprocessor directive you will meet is probably the
directive. What it does is takes all of something
and inserts it in your file where the directive was. The hello world program starts with the line
This line adds the functions and objects that let you use the standard input and output.
The C language, which also uses the preprocessor, does not have as many header files as the C++ language, but in C++ you can use all the C header files.
The next important directive is probably the
directive. This tells the preprocessor that as it goes along the file, it should replace every occurrence of something
with something_else
. It can also make things similar to functions, but that probably counts as advanced C++.
The something_else
is not needed, but if you define something
as nothing, then outside preprocessor directives, all occurrences of something
will vanish.
This actually is useful, because of the #if
,#else
and #ifdef
directives. The format for these would be the following:
These directives insert the code that is in the true bit, and deletes the false bits. this can be used to have bits of code that are only included on certain operating systems, without having to rewrite the whole code.
# Remarks
The 'Hello World' program is a common example that can be simply used to check compiler and library presence. It uses the C++ standard library, with std::cout
from <iostream>
, and has only one file to compile, minimizing the chance of possible user error during compilation.
The process for compiling a C++ program inherently differs between compilers and operating systems. The topic Compiling and Building contains the details about how to compile C++ code on different platforms for a variety of compilers.