Dev C%2b%2b Templates
Templates are powerful features of C which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates. Templates are often used in larger codebase for the purpose of code reusability and flexibility of the programs.
- C++ Basics
- C++ Object Oriented
- The Standard Template Library (STL) is one of the most essential features of C. It has very much grown in recent years. Basically, the Standard Template Library provides templatized, general-purpose classes as well as methods. These classes and functions/methods implement several popular and most commonly used algorithms as well as data.
- Dev-C is a free IDE that uses the MinGW or TDM-GCC 64-bit compiler. A free C compiler comes with Dev-C. Dev-C makes installing the C compiler easy. Dev-C uses DevPaks. DevPaks is extensions on the IDE with additional libraries, templates, and utilities. DevPaks often contains, GUI utilities, and GTK+, wxWidgets, and FLTK toolkits.
- C++ Advanced
- C++ Useful Resources
- Selected Reading
Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.
A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>.
You can use templates to define functions as well as classes, let us see how they work −
Function Template
The general form of a template function definition is shown here −
Here, type is a placeholder name for a data type used by the function. This name can be used within the function definition.
The following is the example of a function template that returns the maximum of two values −
If we compile and run above code, this would produce the following result −
Class Template
Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here −
Here, type is the placeholder type name, which will be specified when a class is instantiated. You can define more than one generic data type by using a comma-separated list.
Following is the example to define class Stack<> and implement generic methods to push and pop the elements from the stack −
If we compile and run above code, this would produce the following result −
I am having a problem with C++ templates which is actually quite embarrassing for me to admit to since I predict a lot of you will figure my problem out faster than it took me to write this post but I've exhausted my patience for right now. I wrote this short snippet in Linux with GCC.
HelloWorld.h
I get the following errors:
'HelloWorld.cc:5: error: expected init-declarator before '<' token'
'HelloWorld.cc:5: error: expected ';' before '<' token'
- 2 Contributors
- forum5 Replies
- 814 Views
- 1 Day Discussion Span
- commentLatest PostLatest Postby Narue
Recommended Answers

What line does the error refer to, and how are you compiling your program?
Jump to PostIs there any particular reason you're including the .cc file in the .h file instead of the other way around?
Jump to PostDev C 2b 2b Templates Download
All 5 Replies
Dev C 2b 2b Templates Pdf
What line does the error refer to, and how are you compiling your program?