• Advertisement
Sign in to follow this  

Calling template functions

This topic is 1584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Edited by SiCrane

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement