Help with Templates

I have to create a project that includes the template function sort3. it must display floats small = 12.3, mid =5.4, big =9.1. I keep getting unqualified id errors. what am i doing wrong?

[code]

template (class T)
class floats
{

T small, mid, big;
public:
floats (T small, T mid, T big);
{
a=first;
b=second;
c=third;
T sort3();

};

template (class T)
T floats(T)::sort3()
{
T sort3;
sort3= a>b, b>c a : b; b : c;
return sort3;
}

int main()
{

floats(int) small(12.3) mid(5.4) big(9.1)
cout << small << " \ " << mid << " \ " << big;
return 0;
}


1. you are using a,b,c without declaring them.
2. You are using first, second, third without declaring them
3. You are taking small, mid, bit as parameters (which is a little confusing)
4. Your floats class is missing a }
5. a>b, b>c a : b; b: c; is not c++
6. return sort3 doesn't make much sense in C++, I assume you are coming from Visual Basic.

I think this is what you want:
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
#include <iostream>
using std::cout;

template<class T>
class floats
{
  void sort3 () // Private method
  {
    if (big < mid)   swap(mid, big);
    if (mid < small) swap(mid, small);
    if (big < mid)   swap(mid, big);
  }

  void swap(T& a, T& b) // Private method
  {
    T temp = a;
    a = b;
    b = temp;
  }
public:
  T small; // Public members
  T mid;
  T big;

  floats(T a, T b, T c)  // Constructor
  {
    small = a;
    mid = b;
    big = c;
    sort3();
  }
};

int main()
{
    floats<double> f(12.3, 5.4, 9.1);
    cout << f.small << " \\ " << f.mid << " \\ " << f.big;
    return 0;
}
Last edited on
^
1. You don't assure the invariant (the client may modify the values as he pleases)
2. Your swap() member function does not use/modify the state of the object.
3. You could use std::swap() instead of losing time codifying your own.
4. You don't use initialization list, so the template type must have a default constructor.
5. `floats' is not a descriptive name.
Topic archived. No new replies allowed.