Sign in to follow this  
Followers 0
Six222

Calling template functions

2 posts in this topic

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

Edited by Six222
0

Share this post


Link to post
Share on other sites

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.

Edited by SiCrane
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0