Find Error in the C++ Program Regarding Templates.

Asked By 0 points N/A Posted on -
qa-featured

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');
 
}
SHARE
Best Answer by james90
Best Answer
Best Answer
Answered By 0 points N/A #119867

Find Error in the C++ Program Regarding Templates.

qa-featured

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. 

Answered By 0 points N/A #119868

Find Error in the C++ Program Regarding Templates.

qa-featured

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.

Related Questions