Template chrono problem

I have the following problem. I attempt a timer using "chrono" attribute-based templates.

The problem is that when splitting into classes in separate files. I get an error.



undefined reference to 'mypair<std::chrono::duration<long long, std:ratio<111, 100011> > >::getmax();


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
This code works just as expected.

 

// class templates

 

#include <chrono>

#include <iostream>

#include <cstdlib>

 

using namespace std;

using namespace std::chrono;

 

template <class T>

class mypair

{

    T a, b;

public:

    mypair() {}

 

    mypair (T first, T second)

    {

        a=first;

        b=second;

    }

 

    T getmax ();

};

 

template <class T>

T mypair<T>::getmax ()

{

    return a;

}

 

int main ()

{

    mypair <milliseconds> myobject ((milliseconds)58080, (milliseconds)75);

    cout << myobject.getmax().count();

    return 0;

}


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
But when I divide it, I get the following error:
//main.c

// class templates

 

#include <chrono>

#include <iostream>

#include <cstdlib>

#include "timer.h"

using namespace std;

using namespace std::chrono;

 

 

 

int main ()

{

    mypair <milliseconds> myobject ((milliseconds)58080, (milliseconds)75);

    cout << myobject.getmax().count();

    return 0;

}

 

//timer.h

template <class T>

class mypair

{

    T a, b;

public:

    mypair() {}

 

    mypair (T first, T second)

    {

        a=first;

        b=second;

    }

 

    T getmax ();

};

 

//timer.c

#include "timer.h"

template <class T>

T mypair<T>::getmax ()

{

    return a;

}


The solution to the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//timer.c

#include "timer.h"

template <class T>

T mypair<T>::getmax ()

{

    return a;

}

template class mypair<nanoseconds>;
template class mypair<microseconds>;
template class mypair<milliseconds>;
template class mypair<seconds>;
template class mypair<minutes>;
template class mypair<hours>;

This only work with this case, but you can do it for other to.

For more information.
Last edited on
Template functions have to be declared and defined in the same file.

PS: As an aside, I was going to say how I've lost count of how many times I've said this, and then I Googled the above sentence and found I had already said it almost exactly a year ago.
Last edited on
Thanks for your help.
However, I still found a way by which the files can remain split.

1
2
3
4
5
6
7
8
9
//Just add this at the end of the .cpp file

template class mypair<nanoseconds>;
template class mypair<microseconds>;
template class mypair<milliseconds>;
template class mypair<seconds>;
template class mypair<minutes>;
template class mypair<hours>;


This only work with this case, but you can do it for other to.

For more information.

http://stackoverflow.com/questions/8752837/undefined-reference-to-template-class-constructor
Yes, that works too, but it's not too different from just moving the definition to the header. IMO, it's worse because you have to add one more line for every unique usage of the template. If you move the definition to the header you can simply forget about it.
Topic archived. No new replies allowed.