template + class question

let's say i declared a template and a class in a header file, how do I define them in a cpp file?

here's my h file
1
2
3
4
5
6
7
8
9
10
11
12
template <class time>
class clock{
 private:
  int hours;
  int minutes;
  int seconds;

 public:
  Time();
  Time(int h, int m, int s);
  void setHours(int h);
}


if the above code were just a straight up class, i'd define setHours as follows
 
void clock::setHours(int h){hours = h;}


but since it's part of a template, how do i do that?
You can't. Define the class template's member functions within the header file.

Templates are C++'s primary code generation tool. When you write a template, you're writing instructions that tell the C++ compiler how to write code for you. The compiler can't follow those instructions when they're in a different file (translation unit).
Last edited on
Well, the short answer is that you don't define them in a .cpp file, because the compiler requires the full definition of a template to be visible when compiling a compilation unit.

But your other issue is that I don't see why you're creating a template here to begin with. What problem are you trying to solve by making your clock class a template? You have a template parameter called "time", but you never use this anywhere in the class template itself.

Also, your class template is called clock, but you seem to be trying to make a constructor called "Time", which doesn't match the name of your class.

In other words, why not just have:
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
class clock {
 private:
  int hours;
  int minutes;
  int seconds;

 public:
  clock();
  clock(int hours, int minutes, int seconds);
  void setHours(int h);
};

clock::clock() : hours(0), minutes(0), seconds(0)
{ }

clock::clock(int hours, int minutes, int seconds)
: hours(hours), minutes(minutes), seconds(seconds)
{ }

void clock::setHours(int h)
{
    hours = h;   
}

int main()
{
    clock ticktock(12, 45, 56);
    ticktock.setHours(3);
}


Here's an example of using a template:
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
template <typename T>
class Thing {
 public:
  Thing(T value);
  
  void setValue(T value);
  
  T value;
};

template <typename T>
Thing::Thing(T value)
: value(value)
{ }

template <typename T>
void setValue(T value)
{
    this->value = value;   
}

int main()
{
    Thing<int> thing(42)
    thing.setValue(43);
}
@mbozzi so i have to declare and define them in one file? when we learned classes, we were taught to separate definition from declaration -- i thought it'd be the same when templates are thrown into the thingymajig.



@Ganado the code i provided is just an example. we're learning templates, FIFO, and LIFO. so based on your example, I need to write template <typename T> every time i define something outside the class Thing?
to have a separate cpp for the template functions
explicit template instantiation: http://www.cplusplus.com/forum/articles/14272/
caveat, you need to define all the types that you template may be used (if you try to use on another type you'll get an "undefined reference" link error)


> so i have to declare and define them in one file?
you may separate the definition in another file and simple #include it at the bottom
(consider include like copy-paste)
1
2
3
4
5
6
//foo.h
#pragma once
template <class T>
T foo(T value);

#include "foo_impl.h 
1
2
3
4
5
6
//foo_impl.h
#pragma once
template <class T>
T foo(T value){
   return value;
}


> I need to write template <typename T> every time i define something outside the class Thing?
yes
Topic archived. No new replies allowed.