Templates

On Line 25, it states that Bigger is not a type - why is that?

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
#include <iostream>
using namespace std;

template <class AnyDataType>
class Bigger
{
 public:
        Bigger(AnyDataType firstNumber, AnyDataType secondNumber)
        {
                    number1=firstNumber;
                    number2=secondNumber;
        }
        AnyDataType bigger()
        {
         return (number1>number2)?number1:number2;
        }
        
 private:
         AnyDataType number1;
         AnyDataType number2;
};

int main()
{
     Bigger<int> *b = new Bigger(5,10);//error.
     cout<<b->bigger()<<endl;
     return 0;
}

closed account (zb0S216C)
It's because you've used "class" as a type not "typename". "class" cannot hold numerical types.

Replace line 4 with this line:
 
template <typename AnyDataType>


You should only use "class" instead of "typename" if you are passing classes.
Last edited on
that didn't work :(
You really should get a default constructor...

Anyways. Look carefully at how you use your constructor. Are you missing something? Maybe another set of these? <>

-Albatross
closed account (zb0S216C)
Albatross is right. You need another <int> in line 25.
1
2
// In line 25.
Bigger< int > *b = new Bigger< int >( 5, 10 );

Thanks! That solved it.
Topic archived. No new replies allowed.