Jump to content
  • Advertisement
Sign in to follow this  
Asuralm

A template problem

This topic is 3806 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

Hi all: I want to declare a set of tempalted functions not in a class, but to use it globally and generally. My code looks like:
template <class T>
void Minmax(const vector<T>&arr, T& minn, T& maxx){
	T x = arr[0], y = arr[0];
	
	for (unsigned int i=1; i < arr.size(); i++){
		if (y < arr) y = arr;
		if (arr < x) x = arr;
	}
	minn = x;
	maxx = y;
	
}



And I want to use it as in this way:
vector<my_class> my_array;
my_class minn, maxx;
Minmax(my_array, minn, maxx);


But it didn't work. Could anyone help me with this please? Thanks [Edited by - Asuralm on February 15, 2008 8:06:06 AM]

Share this post


Link to post
Share on other sites
Advertisement
What didn't work?

Did it compile?

Is it a run-time problem?

We need more information, can you paste the error codes please?

Share this post


Link to post
Share on other sites
Sorry, it didn't compile, the error message is:



undefined reference to `void Minmax<my_class>(std::vector<my_class > const&, my_class&, my_class&)'





I am sure that I have included the header files correctly.

Share this post


Link to post
Share on other sites
I gave it a quick try under both GCC and MSVC9 and both built just fine.

What I would suggest is you may have a forward declaration of the template function somewhere and your method that is making use of it can't actually see the implementation, which it will need to generate template code for the that template argument (my_class in this case).

Template code needs to be in headers, unless you explicitly instantiate for each type you will use it with (which is sometimes useful if it's a small set).

The other possibility is you could be using an old compiler. If you're using Visual Studio 6 or something, I could envision it having issues.

Share this post


Link to post
Share on other sites
Thanks Rydinare, the problem was that I seperate the template declaration and the template code.

Thanks again.

Share this post


Link to post
Share on other sites
Quote:
Original post by Asuralm
Thanks Rydinare, the problem was that I seperate the template declaration and the template code.

Thanks again.


Happy to be of assistance.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!