Calling template functions

Started by
1 comment, last by Six222 10 years, 7 months ago

I'm having an issue with syntax and I was wondering if anyone could explain why the following code counts as a redefinition and doesn't work as expected?


int z;

template<class T>
void add(T x, T y) {
	z = x + y;
}

add<int>(100, 50); // Error C2365

int main() {

	cout << z << endl;

	return 0;
}

Thanks smile.png

Advertisement

You can't call functions outside of function definitions, so the compiler looks at the function call and tries to figure out what you're trying to do and is guessing that you're trying to redefine the template rather than call a function.

Edit: Well to be pedantic, you can have a function call as part of a variable definition, but the compiler would need to see the variable definition first.

Oh, I always thought you could call functions like that.. Thanks for the quick response!

This topic is closed to new replies.

Advertisement