Jump to content

  • Log In with Google      Sign In   
  • Create Account


Calling template functions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Six222   Members   -  Reputation: 439

Like
0Likes
Like

Posted 20 September 2013 - 12:58 PM

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, 20 September 2013 - 12:59 PM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9495

Like
1Likes
Like

Posted 20 September 2013 - 01:03 PM

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, 20 September 2013 - 01:04 PM.


#3 Six222   Members   -  Reputation: 439

Like
0Likes
Like

Posted 20 September 2013 - 01:08 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS