Sign in to follow this  

A template problem

This topic is 3591 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[i]) y = arr[i];
		if (arr[i] < x) x = arr[i];
	}
	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
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
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

This topic is 3591 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.

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