Problem in Template Classes

1:- In Template class : template <class khan, class khan1, class khan2> if I removed the other two arguments like template<class khan> and then i modify the two functions like below so it works like an ordinary functions,

Below Program Works Fine like and ordinary class and functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 #include <iostream>
using namespace std;

class generic {
    public:
template <class khan> //Generic Type of data//
khan add(khan a, khan b ,khan c){
  khan d = a+b+c;
   cout <<"Function 1:" <<endl;
  cout <<"Integers Addition: = " <<d <<endl;
  cout<<"\n";
  return d;
}

double add1(double a, double b ,double c){
 double d = a+b+c;
 cout <<"Function 2:" <<endl;
  cout <<"Double Inegers Addition: = " <<d <<endl;
   cout<<"\n";
  return d;
}

float per(float a, float b ,float c){
 float d = a+b+c;
 float per = 0;
 per = d*100.0/300;
 cout <<"Function 3:" <<endl;
cout <<"Total Marks are = " << d  <<endl;
cout <<"Percentage = " << per <<"%"  <<endl;
  return per;
}
};

int main()
{
    generic g;
    g.add(5,5,5);
    g.add1(5.5,5.5,5.5);
    g.per(78,89,89);


    return 0;
}


Program :2 Which doent work: Problem/Output= "Does Not Name A type or Mis match call to a Function""" Please any1 sort it out...and guide me where i m making mistakes and mess....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  #include <iostream>
using namespace std;

class generic {
    public:
template <class khan, class khan1, class khan2> //Generic Type of data//

khan add(khan a, khan b ,khan c){
  khan d = a+b+c;
   cout <<"Function 1:" <<endl;
  cout <<"Integers Addition: = " <<d <<endl;
  cout<<"\n";
  return d;
}

khan1 add1(khan1 a, khan1 b ,khan1 c){
 khan1 d = a+b+c;
 cout <<"Function 2:" <<endl;
  cout <<"Double Inegers Addition: = " <<d <<endl;
   cout<<"\n";
  return d;
}

khan2 per(khan2 a, khan2 b ,khan2 c){
 khan2 d = a+b+c;
 khan2 per = 0;
 per = d*100.0/300;
 cout <<"Function 3:" <<endl;
cout <<"Total Marks are = " << d  <<endl;
cout <<"Percentage = " << per <<"%"  <<endl;
  return per;
}
};

int main()
{
    generic g;
    g.add(5,5,5);
    g.add1(5.5,5.5,5.5);
    g.per(78,89,89);


    return 0;
}
I think you are confused on the proper use of template syntax. Please take time to read this link and then let us know if you make any progress.
http://www.cplusplus.com/doc/tutorial/templates/
Thank you kevinKjt2000,, yeah sorry i was little bit confused ,, but anhow i got now some idea about it... but now i have another problem:
please check it out and correct it,,

Problem When i run this program at the end after displaying LETS PLAY THE GAME it show zero so how to remove that Zero, I used return 1 so it showed 1 and then i removed the return statement in my function/method name Print() then i showed a huge value dont know whats the problem.......
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;
template <class T>
class khan{
T first, second;
public:
    khan(){
        T a,b;
    cout<<"Enter Vale for a"<<endl;
    cin>>a;
    cout<<"Enter Vale for b"<<endl;
    cin>>b;
    first  = a;
    second = b;
    }
    T compare();
    T print();
};

template <class T>
T khan<T>::compare(){
if(first>second){
        T i= first+2; //return type is T which is now Integer in this case//
        return(i);}
else{
        if(first<second){
        T j= first-2; //return type is T which is now Integer in this case//
        return(j);}
}
}

template <class T>
T khan<T>::print(){
cout<<"\nLets Play the Game"<<endl;
return 0 ;
}


int main()
{
    khan <int> k;
    cout<<k.compare();
    cout<<k.print();
}
cout<<k.print();
¿what do you think that does?

Easier, ¿what would code<<foo(); do?
Ne555 sorry I didn,t get you,, would you please tell me how exactly to implement the code<<foo(),,, I used cout<<k.print() to call the Print() function i used above,,, please guide me ...
Don't use cout<<k.print() instead just do k.print(). Reason: in the x::print() method it already has the cout, so there's going to be an error repeating the ostream. Its like saying std::cout<<std::cout<<"blah";
Oops, sorry I made a typo
What I mean was cout<<foo();
that would execute `foo()', and then print to screen whatever foo() returns (like in cout << k.compare();

So analyze what cout<<k.print(); is doing.
`k.print()' prints a message and returns 0, that it's printed by cout.


If you don't want to output the result, then don't output the result (¿why are you returning anything, btw?)
Topic archived. No new replies allowed.