Jump to content

View more

Image of the Day

Isn't this a lovely apple tempart placeholder thing  #gamedev worth a #screenshotsaturday even I would say. https://t.co/fQH1d0ySIG
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Calling template functions

2: Adsense

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   

453
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.


#2 SiCrane   Moderators   

11705
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   

453
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.