Unresolved externals - template

Hello,
I have a little problem with my program in C++. Please, could you help me? Thank you! :)

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
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>

using namespace std;

template <typename T>
class Trida
{
	T one;
	T two;
	int number;

public:
	Trida () {one = "one"; two = "two"; number = 0;}
	Trida (T o, T t, int n);
	friend ostream & operator << (ostream & os, Trida &l);
	inline void show () const;
};

template <typename T>
ostream & operator << (ostream & os, Trida<T> &l)
{
	cout << one << endl;
	cout << two << endl;
	cout << number << endl;
	return os;
}

template <typename T>
Trida<T>::Trida(T o, T t, int n)
{
	one = o;
	two = t;
	number = n;
}

template <typename T>
void Trida<T>::show () const
{
	cout << one << endl;
	cout << two << endl;
	cout << number;
}

int main ()
{
	Trida<string> al ("xOne","xTwo",111);
	al.show();
	cout << al;

	system("pause");
	return 0;
}
Check out the second response here: http://stackoverflow.com/questions/1297609/overloading-friend-operator-for-template-class
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
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>

using namespace std;

template <typename T> class Trida;

template <typename T>
ostream& operator<<(ostream & os, Trida<T> &l)
{
    os << l.one << endl;
    os << l.two << endl;
    os << l.number << endl;
    return os;
}

template <typename T>
class Trida
{
    T one;
    T two;
    int number;

public:
    Trida () {one = "one"; two = "two"; number = 0;}
    Trida (T o, T t, int n);
   
    //notice the extra angle brackets right before the parameters!
    friend ostream& operator<< <> (ostream & os, Trida<T> &l);
    inline void show () const;
};

template <typename T>
Trida<T>::Trida(T o, T t, int n)
{
    one = o;
    two = t;
    number = n;
}

template <typename T>
void Trida<T>::show () const
{
    cout << one << endl;
    cout << two << endl;
    cout << number;
}

int main ()
{
    Trida<string> al ("xOne","xTwo",111);
    al.show();
    cout << al;

    system("pause");
    return 0;
}
Last edited on
Thank you very much ! :)
Topic archived. No new replies allowed.