Consider a c++ template funtion
template<class T>
T& Add(T a, T b){return a+b ;}

if this function is called as
T c = Add("SAM", "SUNG");

what will happen? What is the problem in the template
declaration/ How to solve the problem.

Answer Posted / sv

1) Compiler will throw the error as C++ doesnt support +
operator for string. Another point is T c = Add
("SAM","SUNG"), where the function call assign to a
TEMPLATE object type, it syntactical error.

2)We need to write a seperate add function as given below

char* Add( const char *s1, const char*s2);

This will solve the problem

Is This Answer Correct ?    3 Yes 8 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Differentiate between declaration and definition.

589


What is a hash function c++?

556


What is pointer -to-members in C++? Give their syntax?

599


What is #include iostream?

744


What's the hardest coding language?

576






write asingle linked list which read from two list & the do the following 1 sort the prime & nonprime num (prime should be less tn nonprime) 2 each node has a prime num followd by nonprime 3 add a new node into its sutable plce 4 erase the most three duplicated non prime num 5 find the least duplicated prime num

2201


What is new in c++?

582


What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator?

640


How the programmer of a class should decide whether to declare member function or a friend function?

687


Can a constructor return a value?

577


Can recursive program be written in C++?

635


What do you mean by inheritance in c++?

609


What does n mean in c++?

634


What function initalizes variables in a class: a) Destructor b) Constitutor c) Constructor

679


What are the restrictions apply to constructors and destructors?

645