Asked By
swanandsaw
0 points
N/A
Posted on - 08/29/2011
I cannot Find the error in the following program . The following program is an example of Templates in C++.
C++ supports a mechanism known as template to implement the concept of generic programming.
Templates allows usto generate a family of classes or a family of functions to handle different data types.
Find the error in the code used for templates.
Â
#include<iostream.h>
Â
template <class T1, class T2>
Â
class Person
Â
{
Â
T1 m_t1;
Â
T2 m_t2;
Â
public:
Â
     Persson (T1 t1, T2 t2)
Â
{
Â
m_t1= t1;
Â
m_t2= t2;
Â
cout<<m_t1<<" "<<m_t2<<endl;
Â
}
Â
Person (T2 t2, T1 t1)
Â
{
Â
m_t2= t2;
Â
m_t1= t1;
Â
cout<<m_t1<<" "<<m_t2<<endl;
Â
}
Â
};
Â
void main()
Â
{
Â
Person<int, float> objPerson1(1, 2.345);
Â
Person<float, char> objPerson2(2.132, 'r');
Â
}
Answered By
james90
0 points
N/A
#119867
Find Error in the C++ Program Regarding Templates.
Swanandsaw,
You have not mentioned the compiler you are using . Sometimes some compilers do not support every type of coding. So you first need to make sure the compiler you are using is suitable for this type of coding.
Â
But my thorough research has shown some fault in your coding.
There is a mistake in line number 15.
ISO c++ forbids declaration of "persson"with no type.
There is also a correction in line 41 . Here "main" must return "Int"
Hope this will be helpful for you.Â
Find Error in the C++ Program Regarding Templates.
Hi Swanandsaw
I can help you better if you post the error you are getting when compiling this code. From the initial observation, I can find a couple of mistakes.
In the constructor you have misspelled class name. It should be ‘Person’.
According to C++ standards, main should be of type integer and not void. It is good practice for a C++ program to return 0 or 1 and hence declaring main as type int.