Template problem

Hi ,

Given the following code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

template <class T>
void print (T obj1 = 0 , T obj2 = 0)
{
	cout << obj1 + obj2 << endl;
}

int main() {

	print();
	return 0;
}


I do not understand why the line "print();" won't compile , but the line
"print(3);" does.
Can you please explain ?
10x,Dave
Last edited on
That is a template function. You need to pass a parameter for the compiler to know how to instantiate it, or you could explicitly pass the template parameter:
 
print<int>();
Last edited on
if you just say print(), then the compiler doesn't know what type you want for T.

If you say print(3), then the compiler can tell that you want T to be int, and therefore it can call the function.

Another way to say which type you want is to say so explicitly:

print<int>();
But the template has default values of zero and zero , ain't that enough ?

10x
No.

What is 'zero'? Zero can be assigned to an int, a bool, a pointer to any type...read a little about templates http://cplusplus.com/doc/tutorial/templates/
Last edited on
Topic archived. No new replies allowed.