Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Getting the error:
error C2027: use of undefined type 'T'

While including a template header with the following definition:
C++
//** Maximum value in a vector **//
template <typename T> 
T max(vector<T>& input,int* index_of_maximum = NULL)
{
	vector<T>::iterator i = input.begin();
	T maximum = *i;
	int index = 0;

	for(i++; i!=input.end(); i++){
		if(*i>maximum)
			maximum = *i;
		index++;
	}

	if(index_of_maximum)
		*index_of_maximum = index-1;

	return maximum;
}


Not sure why the compiler is calling that an error. I have one project where that works fine and another project where it results in that error. Must be some setting that results in this odd error. Any suggestions?
Posted
Comments
CPallini 24-Sep-13 15:57pm    
Probably a test on my system (VS2012) is not useful. However your code compiles and runs fine here.
Albert Holguin 24-Sep-13 16:02pm    
Yeah... I have no idea what's going on... as an interesting side note, I'm not even using that function and the compiler is trying to compile it (don't think it should be since it's a template). Not sure if this is a bug in the compiler. It only happens on MFC projects, projects that are command line don't have this problem.
Albert Holguin 24-Sep-13 16:06pm    
It only gives the error on functions returning type "T". So the compiler must be checking the prototypes but not necessarily doing a full compilation of the function. This definitely smells like a compiler bug (since it works in some projects but not others).
CPallini 24-Sep-13 16:22pm    
By the way 'max' is a dangerous name for a user's function. What happens if you change it?
Albert Holguin 24-Sep-13 16:23pm    
it's enclosed in a namespace

1 solution

There's a macro definition in windef.h that's name max() that resulted in this problem. The code is fine, just need a different name that won't conflict with any MS defined macros.
 
Share this answer
 
Comments
pasztorpisti 24-Sep-13 18:10pm    
+5, In serious projects (especially in crossplatform ones) one of the first things I do in the core libs is #undeffing min and max and implementing my own min/max templates...
Albert Holguin 24-Sep-13 21:10pm    
Thanks... I tend to want to leave MS defined macros alone, but it can sure bit you in the rear-end later on. Wasted at least a couple of hours scratching my head with this beauty. You typically don't see macros labeled like that (lower case name) but this must be old stuff.
Volynsky Alex 25-Sep-13 11:12am    
Nice!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900