Anyone know where is my M+U result go??

#include <iostream>
using namespace std;

template<class P>
P add(P a, P b){
  P result;
  result = a + b;
  return result;
}

int main() {
  int a1 = 5, a2 = 3;
  double b1 = 1.2, b2 = 2.3;
  char c1 = 'M', c2 = 'U';
  string d1 = "Multimedia", d2 = "University";

  cout << a1 << " + " << a2 << " = " << add (a1, a2) << endl
       << b1 << " + " << b2 << " = " << add (b1, b2) << endl
       << c1 << " + " << c2 << " = " << add (c1, c2) << endl
       << d1 << " + " << d2 << " = " << add (d1, d2) << endl;

  return 0;
}

When you use template function for char the definition become char add(char,char)
so it cat only return a single char. but you function returns multiple char 'M'+'U' = 'MU". That why it is showing you wrong output; try char* in place of char.

When you use template function for char the definition become char add(char,char)
so it cat only return a single char. but you function returns multiple char 'M'+'U' = 'MU". That why it is showing you wrong output; try char* in place of char.

thx sundip, but after i change the P add(P a, P b) to P add(P* a, P* b)
the compiler say that "invalid conversion from 'char'to 'char*'"

best regard;

I have a sneaking suspicion this is getting into explicit template specialization. That's something that I'm afraid I'm not well-versed with yet...

It would need to be something to return a null-terminated array of char or an std::string. Inside it you'll have to do some sort of concatenation operation. How you do that depends on the return type.

By mean of changing char to char* is : -
char* c1 = "M";
char* c2 = "U\0";
so for the given parameter definition of template class become P* add(P* a, P* b).

First :

template<class P>
P add(P a, P b){
  P result;
  result = a + b;
  return result;
}

is simply :

template<class P>
P add(P a, P b){
  return a + b;
}

second, what are you expecting when adding chars? Do you expect M + U to equal "MU"?
If so then consider using string as a type instead of char.

@sundip: not a good advice, since now instead of adding chars, he will be trying to
add char pointers, literally! and not its values.
So for this to work, he has to specialize his add function. So rather than complicate things, just use std::string.

First :

template<class P>
P add(P a, P b){
  P result;
  result = a + b;
  return result;
}

is simply :

template<class P>
P add(P a, P b){
  return a + b;
}

second, what are you expecting when adding chars? Do you expect M + U to equal "MU"?
If so then consider using string as a type instead of char.

@sundip: not a good advice, since now instead of adding chars, he will be trying to
add char pointers, literally! and not its values.
So for this to work, he has to specialize his add function. So rather than complicate things, just use std::string.

thx firstPerson, my coding now look more simple than before.
but I not much familiar with the Template Specialization, hmm... but I will try my best to solve the question as I can... thx ^^

I've done some experimenting. I think you might be better off simply writing an overridden version of the function rather than an explicit specialization. The way you have written the template precludes the possibility of returning anything but a single char because 'M' and 'U' are both char literals, not C-Style strings.

If you write a direct override instead of an explicit specialization, you can return a C++-Style std::string, which simplifies things significantly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.